3.Android硬件访问服务编写APP代码

来源:互联网 发布:数据平台开发工程师 编辑:程序博客网 时间:2024/06/05 04:30

1>. 编写APP端的代码

1<. 删除之前HardControl.java

2<. 导入 通过aidl 产生 的Java接口文件(IXxxxService.java)

1<. import android.os.ILedService

2<. 定义ILedService实例化对象: private ILedService iLedService = null;

3<. 获得实例化对象: iLedService = ILedService.Stub.asInterface(ServiceManager.getService(“Led”));

4<. 使用:iLedService.ledCtrl(0, 0);

[发现问题]:

1<. 找不到包 : Error:(6, 18) 错误: 找不到符号 符号:类 ServiceManager 位置:程序包 android.o 解决传送门
[包含什么]:out/target/common/obj/JAVA_LIBRARY/framework_intermediates/calsses.jar
[怎么包含]:相关链接

1<. 第一步将包含的文件拷贝到工程目录

2<. File -> Project Structure -> Alt+insert

3<. 选择Import .JAR/.AAR Package, 并选择包含的文件

4<. 更改依赖:在Project Structure下的Modules->app->Dependencies-> Alt + insert -> 3 -> 选择对于的模块即可

2<. java.lang.OutofMemoryError:GC overhead limit exceeded”,”sources”:[{}]} 解决传送门
[解决方法]:build.gradle android{}里添加

dexOptions {
javaMaxHeapSize “4g”
}

3<. 使用了多个dex : Too many method references 解决传送门
[解决方法]:

1<. build.gradle defaultConfig{}里添加

multiDexEnabled true

2<. build.gradle dependencies {}里添加

compile ‘com.android.support:multidex:1.0.0’

3<. 修改AndroidMainfest.xml 里添加

android:name=”android.support.multidex.MultiDexApplication”

[补充]:

dex : 是Android系统对java文件的一些优化。Android运行的程序并不是原原本本的Java程序,他是把所谓的java源代码转换成了dex格式.

示例代码:

1<. MainActivity.java –APP层

package com.becauseican.app_0001_leddemo;import android.app.Activity;import android.os.Bundle;import android.os.RemoteException;import android.os.ServiceManager;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.Toast;import android.os.ILedService;import java.util.concurrent.Future;public class MainActivity extends Activity {    private Button buttonLead = null;    private boolean boolLead = false;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    private ILedService iLedService = null;    /* 自己定义的类 */    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            boolLead = !boolLead;            if (boolLead) {                buttonLead.setText("Close All");                checkBoxLed1.setChecked(true);                checkBoxLed2.setChecked(true);                checkBoxLed3.setChecked(true);                checkBoxLed4.setChecked(true);                /* 全部点亮 */                for (int i = 0; i < 4; ++i) {                    try {                        iLedService.ledCtrl(i, 1);                    } catch (RemoteException e) {                        e.printStackTrace();                    }                }            }else {                buttonLead.setText("Open All");                checkBoxLed1.setChecked(false);                checkBoxLed2.setChecked(false);                checkBoxLed3.setChecked(false);                checkBoxLed4.setChecked(false);                /* 全部熄灭 */                for (int i = 0; i < 4; ++i) {                    try {                        iLedService.ledCtrl(i, 0);                    } catch (RemoteException e) {                        e.printStackTrace();                    }                }            }        }    }    public void onCheckboxClicked(View view) {        // Is the view now checked?        boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked        try {            switch(view.getId()) {                case R.id.CheckBoxLed4:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed4 on", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(3, 1);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed4 off", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(3, 0);                    }                    // Remove the meat                    break;                case R.id.CheckBoxLed3:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed3 on", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(2, 1);                    }                    // Cheese me                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed3 off", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(2, 0);                    }                    // I'm lactose intolerant                    break;                case R.id.CheckBoxLed2:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed2 on", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(1, 1);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed2 off", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(1, 0);                    }                    // Remove the meat                    break;                case R.id.CheckBoxLed1:                    if (checked) {                        Toast.makeText(getApplicationContext(), "CheckBoxLed1 on", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(0, 0);                    }                    // Put some meat on the sandwich                    else {                        Toast.makeText(getApplicationContext(), "CheckBoxLed1 off", Toast.LENGTH_SHORT).show();                        iLedService.ledCtrl(0, 0);                    }                    // Remove the meat                    break;                // TODO: Veggie sandwich            }        } catch (RemoteException e) {            e.printStackTrace();        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        buttonLead = (Button) findViewById(R.id.ButtonLead);        /* 获得硬件访问服务的实例化对象 */        iLedService = ILedService.Stub.asInterface(ServiceManager.getService("leds"));        checkBoxLed1 = (CheckBox) findViewById(R.id.CheckBoxLed1);        checkBoxLed2 = (CheckBox) findViewById(R.id.CheckBoxLed2);        checkBoxLed3 = (CheckBox) findViewById(R.id.CheckBoxLed3);        checkBoxLed4 = (CheckBox) findViewById(R.id.CheckBoxLed4);/*      // 采用匿名类的方式        buttonLead.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v){                boolLead = !boolLead;                if (boolLead) {                    buttonLead.setText("Close All");                }else {                    buttonLead.setText("Open All");                }            }        });*/        //使用自定义类的方式        buttonLead.setOnClickListener(new MyButtonListener());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}
阅读全文
0 0