andriod驱动之旅-在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(8)
来源:互联网 发布:网络渗透用什么系统 编辑:程序博客网 时间:2024/06/06 03:48
我们在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,下面主例出主要文件:
【注意:从Eclipse拷贝工程过来的时候,要把gen目录删掉,不然R类的定义就会重复了。
从Eclipse拷贝工程到Android源代码目录时,只留下AndroidManifest.xml文件以及res和src文件夹就可以了,其它的文件都可以删掉,然后再往里面添加一个Android.mk文件。模拟器起不来:使用make droidcore代替make snod,重新编译systemimg。
最后:
# emulator -partition-size 512 -kernel /home/skape/Android/kernel/goldfish/arch/arm/boot/zImage -sysdir /home/skape/Android/Android-4.0-share/out/target/product/generic -system system.img -data userdata.img -ramdisk /home/skape/Android/Android-4.0-share/out/target/product/generic/ramdisk.img
即可启动
】
目录:Android-4.0-share\packages\experimental
主程序是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);
- }
- }
- }
- <?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>
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
五. 重新打包系统镜像文件system.img:
重新打包后的system.img文件就内置了Hello.apk文件了。
1962 KB/s (3073460 bytes in 1.529s)
- andriod驱动之旅-在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(8)
- 在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层的硬件服务
- 在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层的硬件服务
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 反向 Ajax,第 1 部分: Comet 简介
- 慎用TCP的选项SO_LINGER
- cocos2d-x中设置颜色
- 【数据结构】 循环单链表 约瑟夫回环问题
- 探索IOS版优酷视频下载
- andriod驱动之旅-在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(8)
- C# and ASP.NET Interview Question and Answers
- C中全局变量和局部变量的初始化问题
- 使用CName记录的好处
- (GCC) Warning: 'typedef' was ignored in this declaration
- 互联网思维这么厉害 你家人知道么?
- cocos2dx 图片黑边 解决
- 揭开web安全测试的神秘面纱(已结束)
- bosh comet ajax