很精练的android系统开发介绍
http://blog.youkuaiyun.com/luoshengyang/article/details/6567257
摘要如下:
app(java)
通过IService.Stub使用service提供的接口 import android.os.IHelloService;
private IHelloService helloService;
helloService = IHelloService.Stub.asInterface(ServiceManager.getService("hello"));
helloService.setVal(val);
"hello": 系统启动时加载HelloService时指定IHelloService.Stub IHelloService.aidl生成
framework service(java)(基于AIDL)
对上:提供java接口,注册service服务对下:调用jni提供的native接口
frameworks/base/core/java/android/os
IHelloService.aidl:生成IHelloService.Stub interface IHelloService {
void setVal(int val);
int getVal();
}
frameworks/base/services/java/com/android/server
HelloService.java:实现java接口到nativa接口转换 public class HelloService extends IHelloService.Stub {
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:增加加载HelloService的代码
ServiceManager.addService("hello", new HelloService());
framework JNI接口(c++)(基于JNI)
对上:实现、注册native接口,实现java环境变量转换对下:调用HAL接口
frameworks/base/services/jni
com_android_server_HelloService.cpp:对应com.android.server.HelloService
java接口的本地实现(基于HAL接口);jni注册;java变量转换
namespace android
{
struct hello_device_t* hello_device = NULL;
static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
hello_device->set_val(hello_device, val);
}
static jint hello_getVal(JNIEnv* env, jobject clazz) {
hello_device->get_val(hello_device, &val);
}
static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {
return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
}
static jboolean hello_init(JNIEnv* env, jclass clazz) {
hello_module_t* module;
if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {
if(hello_device_open(&(module->common), &hello_device) == 0) {
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> }
}
static const JNINativeMethod method_table[] = {
{"init_native", "()Z", (void*)hello_init},
{"setVal_native", "(I)V", (void*)hello_setVal},
{"getVal_native", "()I", (void*)hello_getVal},
};
int register_android_server_HelloService(JNIEnv *env) {
return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
}
};
HAL接口(c)(基于android HAL框架)
对上:实现驱动隔离(用户控件)对下:调用驱动
hardware/libhardware/include/hardware
struct hw_module_t common
struct hw_device_t common
hardware/libhardware/modules
struct hello_module_t HAL_MODULE_INFO_SYM静态定义
hello_device_open动态设置struct hello_device_t