在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。
一. 参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。
二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。使用Eclipse的Android插件ADT创建Android工程很方便,这里不述,可以参考网上其它资料。工程名称为Hello,下面主例出主要文件:
主程序是src/shy/luo/hello/Hello.java:
package shy.luo.hello; import shy.luo.hello.R; import android.app.Activity; import android.os.ServiceManager; import android.os.Bundle; import android.os.IHelloService; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Hello extends Activity implements OnClickListener { private final static String LOG_TAG = "shy.luo.renju.Hello"; private IHelloService helloService = null; private EditText valueText = null; private Button readButton = null; private Button writeButton = null; private Button clearButton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); helloService = IHelloService.Stub.asInterface( ServiceManager.getService("hello")); valueText = (EditText)findViewById(R.id.edit_value); readButton = (Button)findViewById(R.id.button_read); writeButton = (Button)findViewById(R.id.button_write); clearButton = (Button)findViewById(R.id.button_clear); readButton.setOnClickListener(this); writeButton.setOnClickListener(this); clearButton.setOnClickListener(this); Log.i(LOG_TAG, "Hello Activity Created"); } @Override public void onClick(View v) { if(v.equals(readButton)) { try { int val = helloService.getVal(); String text = String.valueOf(val); valueText.setText(text); } catch (RemoteException e) { Log.e(LOG_TAG, "Remote Exception while reading value from device."); } } else if(v.equals(writeButton)) { try { String text = valueText.getText().toString(); int val = Integer.parseInt(text); helloService.setVal(val); } catch (RemoteException e) { Log.e(LOG_TAG, "Remote Exception while writing value to device."); } } else if(v.equals(clearButton)) { String text = ""; valueText.setText(text); } } }
程序通过ServiceManager.getService("hello")来获得HelloService,接着通过IHelloService.Stub.asInterface函数转换为IHelloService接口。其中,服务名字“hello”是系统启动时加载HelloService时指定的,而IHelloService接口定义在android.os.IHelloService中,具体可以参考在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文。这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过IHelloService.getVal和IHelloService.setVal两个接口实现。
界面布局文件res/layout/main.xml和字符串文件res/values/strings.xml及程序描述文件AndroidManifest.xml参考下面链接。
https://blog.csdn.net/yzhang8703/article/details/7163409
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
五. 重新打包系统镜像文件system.img:
重新打包后的system.img文件就内置了Hello.apk文件了。

最后更新:2017-04-03 12:54:58
上一篇:
在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
下一篇:
在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务
政府安全资讯精选 2017年第十三期 网信办发布《互联网新闻信息服务新技术新应用安全评估管理规定》;Facebook颁布新广告政策,加强内容安全
配置Mysql用户权限命令
H5响应式网站建设该如何规划
动态创建script标签实现跨域资源访问的方法
第3章 对象基础
阿里云启动天池电力AI大赛
互联网医疗投资最需警惕“热钱” 流量“质”比“量”靠谱
Qt的内存管理
[ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?
java 后台框架 支持APP接口调用 APP后台 java springmvc mybaits mysql oracle html5 后台框架 手机后台框架