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); }}
- 3.Android硬件访问服务编写APP代码
- Android硬件访问服务框架代码编写
- Android 编写硬件访问服务
- 2.Android硬件访问服务编写系统代码
- 4.Android硬件访问服务编写HAL代码
- 编写Android应用程序验证硬件访问服务
- Android硬件访问服务---APK编写
- Android驱动(一)硬件访问服务学习之(四)Android应用程序APP编写
- android app 访问framework硬件服务
- tiny210 hal 6 Android系统中编写APP通过应用程序框架层访问硬件服务。
- Android 硬件访问服务
- Android硬件访问服务
- Android系统之APP访问硬件--------JNI与硬件访问服务0001
- Android 从硬件到应用:一步一步向上爬 6 -- 编写APP测试框架层硬件服务(完)
- Android硬件抽象层编写JNI方法提供Java访问硬件服务接口
- 开发Android硬件访问服务
- 开发Android硬件访问服务
- Android添加硬件访问服务
- Sublime Text 连接 FTP/SFTP
- 排序算法之快速排序
- 566. Reshape the Matrix
- Hadoop2.6.0运行mapreduce之Uber模式验证 标签: hadoopmapreduce源码uberjava 2016-05-05 14:55 19815人阅读 评论(2) 收藏 举报
- Hadoop 新 MapReduce 框架 Yarn 详解
- 3.Android硬件访问服务编写APP代码
- Linux下安装及使用sqlite3数据库
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- Zabbix 基础与进阶(2) —— zabbix工作模式的部署
- 第六弹——好用且开源的mysql
- webservice-自定义注解处理参数加解密问题
- Java内存分配、管理小结
- linux下python如何输入不用按回车
- Java主线程