我們在Android系統增加硬體服務的目的是為了讓應用層的APP能夠通過Java接口來通路硬體服務。那麼, APP如何通過Java接口來通路Application Frameworks層提供的硬體服務呢?在這一篇文章中,我們将在Android系統的應用層增加一個内置的應用程式,這個内置的應用程式通過ServiceManager接口擷取指定的服務,然後通過這個服務來獲得硬體服務。
二. 為了友善開發,我們可以在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");
}
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)) {
String text = valueText.getText().toString();
int val = Integer.parseInt(text);
helloService.setVal(val);
Log.e(LOG_TAG, "Remote Exception while writing value to device.");
else if(v.equals(clearButton)) {
String text = "";
valueText.setText(text);
}
界面布局檔案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:id="@+id/edit_value"
android:hint="@string/hint">
</EditText>
</LinearLayout>
android:orientation="horizontal"
<Button
android:id="@+id/button_read"
android:layout_height="wrap_content"
android:text="@string/read">
</Button>
android:id="@+id/button_write"
android:text="@string/write">
android:id="@+id/button_clear"
android:text="@string/clear">
</LinearLayout>
字元串檔案res/values/strings.xml:
<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:
<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的檔案内容如下:
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應用程式:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5COrZUawMjN3gTN5AzMx8FMvw1ckF2bsBXdvwFdl5mLuR2cj5Set1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)
打開Hello應用程式:
點選Read按鈕,可以從HelloService中讀取硬體寄存器val的值;點選Clear按鈕,可以清空文本框的值;在文本框中輸入一個數值,再點選Write按鈕,便可以将這個值寫入到硬體寄存器val中去,可以再次點選Read按鈕來驗證是否正确寫入了值。