1. 程式人生 > >android各個層次的理解

android各個層次的理解

感覺要理解android系統的架構,最好的辦法是調通一個最簡單程式的工作流程

android 之hello world理解

1.java app層
private IHelloService  Led_demo_service = null;
Led_demo_service = IHelloService.Stub.asInterface(ServiceManager.getService("hello"));
Led_demo_service.setVal(1);


2.中間層
\a33-dvk3\frameworks\base\core\java\android\os\IHelloService.aidl
package android.os;
/** {@hide} */
interface IHelloService {
//dfdfd
void setVal(int val);  
    int getVal(); 
}




3.java服務層
\a33-dvk3\frameworks\base\services\java\com\android\server
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();






4.JNI層
連線java與c層
\a33-dvk3\frameworks\base\services\jni\onload.cpp
register_android_server_HelloService(env);
\a33-dvk3\frameworks\base\services\jni\com_android_server_HelloService.cpp
/*JNI方法表*/
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));
}




 static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
int val = value;
ALOGE("Hello JNI: set value %d to device.", val);
if(!hello_device) {
ALOGE("Hello JNI: device is not open.");
return;
}

hello_device->set_val(hello_device, val);





5.hal層
各個廠商可封裝的層
\a33-dvk3\hardware\libhardware\modules\hello\hello.c


static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));

if(!dev) {
ALOGE("Hello Stub: failed to alloc space");
return -EFAULT;
}


memset(dev, 0, sizeof(struct hello_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = hello_device_close;
dev->set_val = hello_set_val;
dev->get_val = hello_get_val;


if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
return -EFAULT;
}


*device = &(dev->common);
ALOGE("Hello Stub: open /dev/hello successfully.");


return 0;
}




static int hello_get_val(struct hello_device_t* dev, int* val) {
if(!val) {
ALOGE("Hello Stub: error val pointer");
return -EFAULT;
}


read(dev->fd, val, sizeof(*val));


ALOGE("Hello Stub: get value %d from device", *val);


return 0;
}




if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
return -EFAULT;
}
#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"


6.linux核心態
上面5層均為使用者態
-->"/dev/hello"
linux字元裝置驅動程式......