在數字科技日新月異的今天,軟體和硬體的完美結合,造就了智能移動裝置的流行。今天大家對iOS和Android系統的趨之若鹜,一定程度上是由于這兩個系統上有着豐富多彩的各種應用軟體。是以,軟體和硬體的關系,在一定程度上可以說,硬體是為軟體服務的。硬體工程師研發出一款硬體裝置,自然少了軟體工程師為其編寫驅動程式;而驅動程式的最終目的,是為了使得最上層的應用程式能夠使用這些硬體提供的服務來為使用者提供軟體功能。對Android系統上的應用軟體來說,就是要在系統的Application Frameworks層為其提供硬體服務。在前面的幾篇文章中,我們着重介紹了Linux核心層、硬體抽象層和運作時庫層提供的自定義硬體服務接口,這些接口都是通過C或者C++語言來實作的。在這一篇文章中,我們将介紹如何在Android系統的Application
Frameworks層提供Java接口的硬體服務。
二. 在Android系統中,硬體服務一般是運作在一個獨立的程序中為各種應用程式提供服務。是以,調用這些硬體服務的應用程式與這些硬體服務之間的通信需要通過代理來進行。為此,我們要先定義好通信接口。進入到frameworks/base/core/java/android/os目錄,新增IHelloService.aidl接口定義檔案:
USER-NAME@MACHINE-NAME:~/Android$ cd frameworks/base/core/java/android/os
USER-NAME@MACHINE-NAME:~/Android/frameworks/base/core/java/android/os$ vi IHelloService.aidl
IHelloService.aidl定義了IHelloService接口:
package android.os;
interface IHelloService {
void setVal(int val);
int getVal();
}
IHelloService接口主要提供了裝置和擷取硬體寄存器val的值的功能,分别通過setVal和getVal兩個函數來實作。
三.傳回到frameworks/base目錄,打開Android.mk檔案,修改LOCAL_SRC_FILES變量的值,增加IHelloService.aidl源檔案:
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
## READ ME: ########################################################
LOCAL_SRC_FILES += /
....................................................................
core/java/android/os/IVibratorService.aidl /
core/java/android/os/IHelloService.aidl /
core/java/android/service/urlrenderer/IUrlRendererService.aidl /
.....................................................................
四. 編譯IHelloService.aidl接口:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base
這樣,就會根據IHelloService.aidl生成相應的IHelloService.Stub接口。
五.進入到frameworks/base/services/java/com/android/server目錄,新增HelloService.java檔案:
package com.android.server;
import android.content.Context;
import android.os.IHelloService;
import android.util.Slog;
public class HelloService extends IHelloService.Stub {
private static final String TAG = "HelloService";
HelloService() {
init_native();
}
public void setVal(int val) {
setVal_native(val);
}
public int getVal() {
return getVal_native();
private static native boolean init_native();
private static native void setVal_native(int val);
private static native int getVal_native();
};
六. 修改同目錄的SystemServer.java檔案,在ServerThread::run函數中增加加載HelloService的代碼:
@Override
public void run() {
....................................................................................
try {
Slog.i(TAG, "DiskStats Service");
ServiceManager.addService("diskstats", new DiskStatsService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting DiskStats Service", e);
}
try {
Slog.i(TAG, "Hello Service");
ServiceManager.addService("hello", new HelloService());
Slog.e(TAG, "Failure starting Hello Service", e);
......................................................................................
}
七. 編譯HelloService和重新打包system.img:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/java
USER-NAME@MACHINE-NAME:~/Android$ make snod
這樣,重新打包後的system.img系統鏡像檔案就在Application Frameworks層中包含了我們自定義的硬體服務HelloService了,并且會在系統啟動的時候,自動加載HelloService。這時,應用程式就可以通過Java接口來通路Hello硬體服務了。我們将在下一篇文章中描述如何編寫一個Java應用程式來調用這個HelloService接口來通路硬體,敬請期待。