老羅版本參見:https://blog.csdn.net/luoshengyang/article/details/6580267
我們在Android系統增加硬件服務的目的是爲了讓應用層的APP能夠通過Java接口來訪問硬件服務。那麼, APP如何通過Java接口來訪問Application Frameworks層提供的硬件服務呢?在這一篇文章中,我們將在Android系統的應用層增加一個內置的應用程序,這個內置的應用程序通過ServiceManager接口獲取指定的服務,然後通過這個服務來獲得硬件服務。
完整代碼見:鏈接:https://pan.baidu.com/s/1DOwrehjEXXiEPmU0DNMumw
提取碼:2jho
一. 參照在Ubuntu上爲Android系統的Application Frameworks層增加硬件訪問服務一文,在Application Frameworks層定義好自己的硬件服務HelloService,並提供IHelloService接口提供訪問服務。
二. 爲了方便開發,我們可以在IDE環境下使用Android SDK來開發Android應用程序。開發完成後,再把程序源代碼移植到Android源代碼工程目錄中。原文中使用Eclipse,現在幾乎所有的項目都使用AndroidStudio了,因此我們是使用AndroidStudio,但是主要文件是一樣的:
主程序是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層增加硬件訪問服務---Android8.0版本實現-對照老羅版本一文。這個程序提供了簡單的讀定自定義硬件有寄存器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>
三. 將src目錄下文件目錄拷貝至packages/apps/Hello目錄,新增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
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
四. 編譯:
zh@ubuntu:sourcecode/trunk/Android/packages/apps/Hello$ mma -j16
編譯成功後,便可以在Android/out/target/product/Hi3571V/system/app目錄下看到Hello.apk文件了。
五. 重新打包系統,系統中就有Hello.apk了。