




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
硬件服务的测试程序我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。一. 原理参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。二. 使用Android SDK来开发Android应用程序为了方便开发,我们可以在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: Hello Value Please input a value. Read Write Clear 程序描述文件AndroidManifest.xml: 三. 将Hello目录拷贝至packages/experimental目录,新增Android.mk文件: :/Android/packages/experimental$ vi Android.mkAndroid.mk的文件内容如下: LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := Helloinclude $(BUILD_PACKAGE)四. 编译::/Android$ mmm packages/experimental/Hello编译成功后,便可以在out/target/product/generic/system/app目录下看到Hello.apk文件了。五. 重新打包系统镜像文件system.img::/Android$ make snod 重新打包后的system.img文件就内置了Hello.apk文件了。六. 运行Android模拟器::/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &在Home Screen中可以看到Hello应用程序:打开Hello应用程序:点击Read按钮,可以从HelloService中读取硬件寄存器val的值;点击Clear按钮,可以清空文本框的值;在文本框中输入一个数值,再点击Write按钮,便可以将这个值写入到硬件寄存器val中去,可以再次点击Read按钮来验证是否正确
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年事业单位招聘考试综合类职业能力倾向测验真题模拟试卷(石家庄)
- 2025年安徽省事业单位招聘考试教师招聘生物学科专业知识试卷及答案
- 呼吸机的考试试题及答案
- 我的探险经历记事类作文(14篇)
- 衡水五升六考试题及答案
- 新解读《GB-T 39316.3-2020军民通 用资源 元数据 第3部分:器材类 航材》
- 2025年中国无绳手持式花园电动工具行业市场分析及投资价值评估前景预测报告
- 2025国考晋中市财务管理岗位申论模拟题及答案
- 2025国考应急部行测言语理解与表达预测卷及答案
- 胃肠疾病早期筛查-洞察与解读
- 集中供电空调客车的应急电源
- LY/T 2663-2016森林防火地理信息系统技术要求
- 2023年江苏银行校园招聘笔试题库及答案解析
- 爆破片安全装置定期检查、使用、维护、更换记录表
- 筑梦航天知识题库
- 质量问题分析改进报告模板
- 抽水蓄能电站建设工程作业指导书编制导则资料
- DB13(J)∕T 105-2017 预应力混凝土管桩基础技术规程
- 私募股权投资基金募集说明书(通用标准版)
- 苯醚酮、-二氟二苯甲酮等建项目资金申请报告写作模板
- 在阳光下成长诗朗诵
评论
0/150
提交评论