我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。

《Android系统源代码情景分析》一书正在进击的程序员网(http://0xcc0xcd.com)中连载,点击进入!

一. 参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。

二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。使用Eclipse的Android插件ADT创建Android工程很方便,这里不述,可以参考网上其它资料。工程名称为Hello,下面主例出主要文件:

主程序是src/shy/luo/hello/Hello.java:

[java] view plain copy
print ?
  1. packageshy.luo.hello;
  2. importshy.luo.hello.R;
  3. importandroid.app.Activity;
  4. importandroid.os.ServiceManager;
  5. importandroid.os.Bundle;
  6. importandroid.os.IHelloService;
  7. importandroid.os.remoteexception;
  8. importandroid.util.Log;
  9. importandroid.view.View;
  10. importandroid.view.View.OnClickListener;
  11. importandroid.widget.Button;
  12. importandroid.widget.EditText;
  13. publicclassHelloextendsActivityimplementsOnClickListener{
  14. privatefinalstaticStringLOG_TAG="shy.luo.renju.Hello";
  15. privateIHelloServicehelloService=null;
  16. privateEditTextvalueText=null;
  17. privateButtonreadButton=null;
  18. privateButtonwriteButton=null;
  19. privateButtonclearButton=null;
  20. /**Calledwhentheactivityisfirstcreated.*/
  21. @Override
  22. publicvoidonCreate(BundlesavedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. helloService=IHelloService.Stub.asInterface(
  26. ServiceManager.getService("hello"));
  27. valueText=(EditText)findViewById(R.id.edit_value);
  28. readButton=(Button)findViewById(R.id.button_read);
  29. writeButton=(Button)findViewById(R.id.button_write);
  30. clearButton=(Button)findViewById(R.id.button_clear);
  31. readButton.setonClickListener(this);
  32. writeButton.setonClickListener(this);
  33. clearButton.setonClickListener(this);
  34. Log.i(LOG_TAG,"HelloActivityCreated");
  35. }
  36. @Override
  37. publicvoidonClick(Viewv){
  38. if(v.equals(readButton)){
  39. try{
  40. intval=helloService.getVal();
  41. Stringtext=String.valueOf(val);
  42. valueText.setText(text);
  43. }catch(remoteexceptione){
  44. Log.e(LOG_TAG,"remoteexceptionwhilereadingvaluefromdevice.");
  45. }
  46. }
  47. elseif(v.equals(writeButton)){
  48. try{
  49. Stringtext=valueText.getText().toString();
  50. intval=Integer.parseInt(text);
  51. helloService.setVal(val);
  52. }catch(remoteexceptione){
  53. Log.e(LOG_TAG,"remoteexceptionwhilewritingvaluetodevice.");
  54. }
  55. }
  56. elseif(v.equals(clearButton)){
  57. Stringtext="";
  58. valueText.setText(text);
  59. }
  60. }
  61. }
程序通过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:
[html] view plain copy
print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical"
  10. android:gravity="center">
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/value">
  15. </TextView>
  16. <EditText
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/edit_value"
  20. android:hint="@string/hint">
  21. </EditText>
  22. </LinearLayout>
  23. <LinearLayout
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="horizontal"
  27. android:gravity="center">
  28. <Button
  29. android:id="@+id/button_read"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="@string/read">
  33. </Button>
  34. <Button
  35. android:id="@+id/button_write"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="@string/write">
  39. </Button>
  40. <Button
  41. android:id="@+id/button_clear"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="@string/clear">
  45. </Button>
  46. </LinearLayout>
  47. </LinearLayout>
字符串文件res/values/strings.xml:
[html] view plain copy
print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">Hello</string>
  4. <stringname="value">Value</string>
  5. <stringname="hint">Pleaseinputavalue...</string>
  6. <stringname="read">Read</string>
  7. <stringname="write">Write</string>
  8. <stringname="clear">Clear</string>
  9. </resources>
程序描述文件AndroidManifest.xml:
[html] view plain copy
print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="shy.luo.hello"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".Hello"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. </manifest>
三. 将Hello目录拷贝至packages/experimental目录,新增Android.mk文件:
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
Android.mk的文件内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Hello
include $(BUILD_PACKAGE)
四. 编译:
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Hello
编译成功后,便可以在out/target/product/generic/system/app目录下看到Hello.apk文件了。
五. 重新打包系统镜像文件system.img:
USER-NAME@MACHINE-NAME:~/Android$ make snod
重新打包后的system.img文件就内置了Hello.apk文件了。
六. 运行Android模拟器:
USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &
在Home Screen中可以看到Hello应用程序:

打开Hello应用程序:


点击Read按钮,可以从HelloService中读取硬件寄存器val的值;点击Clear按钮,可以清空文本框的值;在文本框中输入一个数值,再点击Write按钮,便可以将这个值写入到硬件寄存器val中去,可以再次点击Read按钮来验证是否正确写入了值。
至此,我们就完整地学习了在Android的Linux内核空间添加硬件驱动程序、在Android的硬件抽象层添加硬件接口、在Android的Application Frameworks层提供硬件服务以及在Android的应用层调用硬件服务的整个过程了,希望能为读者进入Android系统提供入门帮助。重新学习整个过程,请参考 Android硬件抽象层(HAL)概要介绍和学习计划。
老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注!

在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务的更多相关文章

  1. HTML5 播放 RTSP 视频的实例代码

    目前大多数网络摄像头都是通过 RTSP 协议传输视频流的,但是 HTML 并不标准支持 RTSP 流。本文重点给大家介绍HTML5 播放 RTSP 视频的实例代码,需要的朋友参考下吧

  2. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  3. 利用Node实现HTML5离线存储的方法

    这篇文章主要介绍了利用Node实现HTML5离线存储的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  4. amazeui模态框弹出后立马消失并刷新页面

    这篇文章主要介绍了amazeui模态框弹出后立马消失并刷新页面,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. 详解如何通过H5(浏览器/WebView/其他)唤起本地app

    这篇文章主要介绍了详解如何通过H5(浏览器/WebView/其他)唤起本地app的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. AmazeUI 折叠面板的实现代码

    这篇文章主要介绍了AmazeUI 折叠面板的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. HTML5手指下滑弹出负一屏阻止移动端浏览器内置下拉刷新功能的实现代码

    这篇文章主要介绍了HTML5手指下滑弹出负一屏阻止移动端浏览器内置下拉刷新功能的实现代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

随机推荐

  1. crontab发送一个月份的电子邮件

    ubuntu14.04邮件服务器:Postfixroot收到来自crontab的十几封电子邮件.这些邮件包含PHP警告.>我已经解决了这些警告的原因.>我已修复每个cronjobs不发送电子邮件(输出发送到>/dev/null2>&1)>我删除了之前的所有电子邮件/var/mail/root/var/spool/mail/root但我仍然每小时收到十几封电子邮件.这些电子邮件来自cronjobs,

  2. 模拟两个ubuntu服务器计算机之间的慢速连接

    我想模拟以下场景:假设我有4台ubuntu服务器机器A,B,C和D.我想在机器A和机器C之间减少20%的网络带宽,在A和B之间减少10%.使用网络模拟/限制工具来做到这一点?

  3. ubuntu-12.04 – 如何在ubuntu 12.04中卸载从源安装的redis?

    我从源代码在Ubuntu12.04上安装了redis-server.但在某些时候它无法完全安装,最后一次makeinstallcmd失败.然后我刚刚通过apt包安装.现在我很困惑哪个安装正在运行哪个conf文件?实际上我想卸载/删除通过源安装的所有内容,只是想安装一个包.转到源代码树并尝试以下命令:如果这不起作用,您可以列出软件自行安装所需的步骤:

  4. ubuntu – “apt-get source”无法找到包但“apt-get install”和“apt-get cache”可以找到它

    我正在尝试下载软件包的源代码,但是当我运行时它无法找到.但是当我运行apt-cache搜索squid3时,它会找到它.它也适用于apt-getinstallsquid3.我使用的是Ubuntu11.04服务器,这是我的/etc/apt/sources.list我已经多次更新了.我尝试了很多不同的debs,并没有发现任何其他地方的错误.这里的问题是你的二进制包(deb)与你的源包(deb-src)不

  5. ubuntu – 有没有办法检测nginx何时完成正常关闭?

    &&touchrestarted),因为即使Nginx没有完成其关闭,touch命令也会立即执行.有没有好办法呢?这样的事情怎么样?因此,pgrep将查找任何Nginx进程,而while循环将让它坐在那里直到它们全部消失.你可以改变一些有用的东西,比如睡1;/etc/init.d/Nginx停止,以便它会休眠一秒钟,然后尝试使用init.d脚本停止Nginx.你也可以在某处放置一个计数器,这样你就可以在需要太长时间时发出轰击信号.

  6. ubuntu – 如何将所有外发电子邮件从postfix重定向到单个地址进行测试

    我正在为基于Web的应用程序设置测试服务器,该应用程序发送一些电子邮件通知.有时候测试是使用真实的客户数据进行的,因此我需要保证服务器在我们测试时无法向真实客户发送电子邮件.我想要的是配置postfix,以便它接收任何外发电子邮件并将其重定向到一个电子邮件地址,而不是传递到真正的目的地.我正在运行ubuntu服务器9.10.先感谢您设置本地用户以接收所有被困邮件:你需要在main.cf中添加:然后

  7. ubuntu – vagrant无法连接到虚拟框

    当我使用基本的Vagrantfile,只配置了两条线:我看到我的虚拟框打开,但是我的流氓日志多次显示此行直到超时:然后,超时后的一段时间,虚拟框框终于要求我登录,但是太久了!所以我用流氓/流氓记录.然后在我的物理机器上,如果我“流氓ssh”.没有事情发生,直到:怎么了?

  8. ubuntu – Nginx – 转发HTTP AUTH – 用户?

    我和Nginx和Jenkins有些麻烦.我尝试使用Nginx作为Jenkins实例的反向代理,使用HTTP基本身份验证.它到目前为止工作,但我不知道如何传递带有AUTH用户名的标头?}尝试将此指令添加到您的位置块

  9. Debian / Ubuntu – 删除后如何恢复/ var / cache / apt结构?

    我在ubuntu服务器上的空间不足,所以我做了这个命令以节省空间但是现在在尝试使用apt时,我会收到以下错误:等等显然我删除了一些目录结构.有没有办法做apt-getrebuild-var-tree或类似的?

  10. 检查ubuntu上安装的rubygems版本?

    如何查看我的ubuntu盒子上安装的rubygems版本?只是一个想法,列出已安装的软件包和grep为ruby或宝石或其他:)dpkg–get-selections

返回
顶部