感觉要理解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字符设备驱动程序......










更多相关文章

  1. MediaPlayer+Stagefright架构(音频)图解
  2. Android上的Ubuntu Debian Armel
  3. Linux入门基础知识(1)-------Linux基础概念
  4. Binder 学习之一
  5. Android Jetpack架构组件简介
  6. Android官方mvp说明——Android架构蓝图——android Architectur
  7. Android官方架构组件Paging-Ex:为分页列表添加Header和Footer
  8. 这里有一份BAT大厂Android面试超详细知识点,收藏备战金九银十
  9. Android内核开发:从源码树中删除出厂的app应用

随机推荐

  1. Android项目创建和项目目录图解
  2. Android(安卓)Canvas drawText实现中文垂
  3. 如何判断 两个不同包名的 Android(安卓)
  4. 现在有三个按钮,在FrameLayout下如何让这
  5. 将Eclipse工程迁移到Android(安卓)Stutio
  6. Android将Widget添加到自己的应用程序
  7. Android(安卓)约束布局ConstraintLayout
  8. android之电话号码查询
  9. 探索react-native run-ios(android)
  10. 始终悬浮在Android屏幕的弹窗