Java Application调用 Frameworks层的硬件服务
来源:互联网 发布:淘宝直播达人淘在哪里 编辑:程序博客网 时间:2024/04/27 21:45
- 综述
- Java应用程序测试Application Frameworks层的硬件服务
- app工程名称为Hello
- 新增Androidmk
- 编译
- 运行Android模拟器
综述
在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。
那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?
在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。
Java应用程序测试Application Frameworks层的硬件服务
app工程名称为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:
<?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>
字符串文件res/values/strings.xml:
<?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>
程序描述文件AndroidManifest.xml:
<?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>
新增Android.mk
将Hello目录拷贝至packages/experimental目录,新增Android.mk文件:
USER-NAME@MACHINE-NAME:~/Android/packages/experimental
include
LOCAL_PACKAGE_NAME := Hello
include $(BUILD_PACKAGE)
编译:
USER-NAME@MACHINE-NAME:~/Android
重新打包后的system.img文件就内置了Hello.apk文件了。
运行Android模拟器:
USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &
在Home Screen中可以看到Hello应用程序:
- Java Application调用 Frameworks层的硬件服务
- Android的Application Frameworks层增加硬件访问服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 为Android系统内置Java应用程序测试Application Frameworks层的硬件服务接口
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 数据分析复盘——基础之SQL语句
- 2015年上半年 软件设计师 上午试卷 综合知识-3
- 为什么CAD打印文字不显示
- Jetson TX1开发笔记(二):TX1开发前必做的几件事
- 解决java代码无法通过vpn访问对端接口
- Java Application调用 Frameworks层的硬件服务
- 关于项目中很多键盘无故弹出或无隐藏的总结
- Angular2--组件生命周期
- 数据包从源主机到达目标主机的过程
- 数据分析复盘——基础之分析工具1(excel)
- Curl不显示统计信息% Total % Received %
- Node.js Express 框架
- (19)servletContext应用:网站计数器
- 开启spring task定时任务后,抛出以下异常