我们在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:

  1. package shy.luo.hello;
  2. import shy.luo.hello.R;
  3. import android.app.Activity;
  4. import android.os.ServiceManager;
  5. import android.os.Bundle;
  6. import android.os.IHelloService;
  7. import android.os.RemoteException;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. public class Hello extends Activity implements OnClickListener {
  14. private final static String LOG_TAG = "shy.luo.renju.Hello";
  15. private IHelloService helloService = null;
  16. private EditText valueText = null;
  17. private Button readButton = null;
  18. private Button writeButton = null;
  19. private Button clearButton = null;
  20. /** Called when the activity is first created. */
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  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, "Hello Activity Created");
  35. }
  36. @Override
  37. public void onClick(View v) {
  38. if(v.equals(readButton)) {
  39. try {
  40. int val = helloService.getVal();
  41. String text = String.valueOf(val);
  42. valueText.setText(text);
  43. } catch (RemoteException e) {
  44. Log.e(LOG_TAG, "Remote Exception while reading value from device.");
  45. }
  46. }
  47. else if(v.equals(writeButton)) {
  48. try {
  49. String text = valueText.getText().toString();
  50. int val = Integer.parseInt(text);
  51. helloService.setVal(val);
  52. } catch (RemoteException e) {
  53. Log.e(LOG_TAG, "Remote Exception while writing value to device.");
  54. }
  55. }
  56. else if(v.equals(clearButton)) {
  57. String text = "";
  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:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Hello</string>
  4. <string name="value">Value</string>
  5. <string name="hint">Please input a value...</string>
  6. <string name="read">Read</string>
  7. <string name="write">Write</string>
  8. <string name="clear">Clear</string>
  9. </resources>
程序描述文件AndroidManifest.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="shy.luo.hello"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".Hello"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android: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)概要介绍和学习计划

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

  1. 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)

    一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...

  2. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)

    在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关系 ...

  3. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6578352 在数字科技日新月异的今天,软件和硬 ...

  4. 为Android系统的Application Frameworks层增加硬件访问服务

    在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两 个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关 ...

  5. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/6578352 在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行 ...

  6. 开发Android系统内置应用小记

    Android系统内置应用可以使用更多的API.更高的权限,与开发普通应用最大的差别在于编译,内置应用编译需要用到Android.mk文件.下面是我在开发过程中的一些小记. 1.在AndroidMai ...

  7. 《Java应用程序(Application)》

    在编写Java应用程序(Application)时可以这样: 1,定义包名. 2, 导入相关的包. 3, 定义一个类. 4,定义相关变量. 5,定义构造函数.(在构造函数内调用init()方法和add ...

  8. 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》

    Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...

  9. 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序(老罗学习笔记2)

    在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中,创建三个不同的文件节点来供用户空间访问,分别是传统的设备文 ...

随机推荐

  1. jetbrain phpstorm 增加或删除一个 live template

    打开\.PhpStorm2016.2\config\templates 将xml文件放入该文件夹中 重启! 单独: setting -> editor -> Live Template +

  2. Chapter 17_2 备忘录函数

    一项通用的编程技术:用空间换时间. 例如有一种做法就可以提高一些函数的运行速度,记录下函数计算的结果,当再次调用该函数时,便可以复用之前的结果. 比如,一个普通服务器,在它收到请求中包含Lua代码,会 ...

  3. matlab读xls数据

    [ndata,label,abalone]=xlsread('data.xls') ndata:表示数字属性 label:表示类别属性 abalone:全部数据

  4. linux内存管理(repost)

    一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...

  5. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  6. 二十六、oracle pl/sql 分页

    一.无返回值的存储过程 古人云:欲速则不达,为了让大家伙比较容易接受分页过程编写,我还是从简单到复杂,循序渐进的给大家讲解.首先是掌握最简单的存储过程,无返回值的存储过程. 案例:现有一张表book, ...

  7. 【CSS】display: inline-block,内联元素

    什么是内联元素? <CSS权威指南>中文字显示:任何不是块级元素的可见元素都是内联元素.其表现的特性是“行布局”形式,这里的“行布局”的意思就是说其表现形式始终以行进行显示.比如,我们设定 ...

  8. 小团队git开发模式

    实验室要使用Git进行代码管理,但是git非常复杂,各种开发模式也是层出不穷.作为新手的偶们很是发囧啊!!网上搜了一下,发现很多并不适合我们小团队运作(它本身就是为Linux内核管理而开发的分布式代码 ...

  9. Android app作为系统应用实现功能笔记

    1.禁用StatusBar相关功能需要添加权限 <uses-permission android:name="android.permission.STATUS_BAR"&g ...

  10. JAVA的字符编码及问题

    web开发时,字符编码及有时候也会是一个麻烦的问题,没有经验的话,肯定不知道怎么解决,有一定的经验的话,那还是比较简单的.以下,是我学习过程中总结出来的几种字符编码级问题和其解决的方法 1.文档乱码, ...