为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两个接口实现。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/value">
- </TextView>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edit_value"
- android:hint="@string/hint">
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/button_read"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/read">
- </Button>
- <Button
- android:id="@+id/button_write"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/write">
- </Button>
- <Button
- android:id="@+id/button_clear"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/clear">
- </Button>
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">Hello</string>
- <string name="value">Value</string>
- <string name="hint">Please input a value...</string>
- <string name="read">Read</string>
- <string name="write">Write</string>
- <string name="clear">Clear</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="shy.luo.hello"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Hello"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
三. 将Hello目录拷贝至packages/experimental目录,新增Android.mk文件:
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
Android.mk的文件内容如下:
四. 编译:
五. 重新打包系统镜像文件system.img:
重新打包后的system.img文件就内置了Hello.apk文件了。


此,我们就完整地学习了在Android的Linux内核空间添加硬件驱动程序、在Android的硬件抽象层添加硬件接口、在Android的
Application
Frameworks层提供硬件服务以及在Android的应用层调用硬件服务的整个过程了,希望能为读者进入Android系统提供入门帮助。重新学习
整个过程,请参考Android硬件抽象层(HAL)概要介绍和学习计划。
为Android系统内置Java应用程序测试Application Frameworks层的硬件服务的更多相关文章
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)
一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...
- 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)
在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关系 ...
- 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6578352 在数字科技日新月异的今天,软件和硬 ...
- 为Android系统的Application Frameworks层增加硬件访问服务
在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两 个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关 ...
- 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6578352 在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行 ...
- 开发Android系统内置应用小记
Android系统内置应用可以使用更多的API.更高的权限,与开发普通应用最大的差别在于编译,内置应用编译需要用到Android.mk文件.下面是我在开发过程中的一些小记. 1.在AndroidMai ...
- 《Java应用程序(Application)》
在编写Java应用程序(Application)时可以这样: 1,定义包名. 2, 导入相关的包. 3, 定义一个类. 4,定义相关变量. 5,定义构造函数.(在构造函数内调用init()方法和add ...
- 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》
Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序(老罗学习笔记2)
在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中,创建三个不同的文件节点来供用户空间访问,分别是传统的设备文 ...
随机推荐
- DBCP数据源的使用
DBCP(DataBase Connection Pool)是一个开源的数据源工具,实际开发直接使用就行了 导入需要的jar包,数据库使用mysql测试
- tcp协议栈
TCP/IP是互联网的核心协议,也是大多数网络应用的核心协议.就前面一段时间面试中问到的TCP/IP问题,这里给出一个简单的小结. TCP由RFC793.RFC1122.RFC1323.RFC20 ...
- cmd 进入mysql
从cmd中进入MySQL的命令界面 1.开始中找出运行:输入cmd2.查找appserv所在盘,我的在D盘,所以接着输入:d:3.在d盘中查找mysql所在目录:cd appserv\mysql\bi ...
- c#中命令copy已退出,返回值为1
c#中命令copy已退出,返回值为1 本正经的道:董姐刚才你说的修心养性其中的'修心'我 有孕在身刚好由戴梦瑶顶替了她的位置按照的指示 ╋旆呆 湎术葶页 邾箕砜笳 烦璜卿廑 奶奶个腿儿的等下次非让你 ...
- Javascript中valueOf与toString区别
前言 基本上,所有JS数据类型都拥有这两个方法,null除外.它们俩解决JavaScript值运算与显示的问题,重写会加大它们调用的优化. 测试分析 先看一例:var aaa = { i: 10, ...
- spring的校验框架 @Validated & BindingResult
controller上写法类似这样: @RequestMapping(value = "saleInfoList.json", method = RequestMethod.GET ...
- jquery学习笔记2——jq效果
一.显示隐藏: 可以使用show()和hide()方法来显示隐藏: $("#hide").click(function(){ $("p").hide(); }) ...
- ios9 新关键字 __kindof 等(etc) 小结
首先__kindof:规定参数为UITableViewCell(举例)这个类或者其子类.比如说一个NSArray<UIView *>*,如果不加__kindof,这个数组只能有UIView ...
- eclipse保存时自动格式化代码和优化导包
- js获取json的value
getJson('age'); function getJson(key){ var jsonObj={"name":"cxr","age" ...