编写并编译硬件访问服务(Application Frameworks层) - muyu01248/log GitHub Wiki
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接口:
[java] view plain copy
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文件:
[java] view plain copy
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();
};
HelloService主要是通过调用JNI方法init_native、setVal_native和getVal_native(见在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口一文)来提供硬件服务。
六. 修改同目录的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());
} catch (Throwable e) {
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