在上一篇文章中,我们看到了如何撰写HAL层的用户硬件驱动程序,我们每也知道,最终该代码会被编译成动态链接库提供给service(jni)使用,那么我们下面来看看service(jni)是如何与HAL通信的。 一般service的jni代码位于framework/base/service/jni/中,我们看看mokoid的ledservice是如何实现的: framework/base/service/jni/com_mokoid_server_LedService.cpp static const JNINativeMethod gMethods[] = { { "_init", "()Z", (void *)mokoid_init }, { "_set_on", "(I)Z", (void *)mokoid_setOn }, { "_set_off", "(I)Z", (void *)mokoid_setOff }, }; int register_mokoid_server_LedService(JNIEnv* env){ static const char* const kClassName = "com/mokoid/server/LedService"; jclass clazz; /* look up the class */ clazz = env->FindClass(kClassName); if (clazz == NULL) { LOGE("Can't find class %s\n", kClassName); return -1; } /* register all the methods */ if (env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK) { LOGE("Failed registering methods for %s\n", kClassName); return -1; } /* fill out the rest of the ID cache */ return 0; } 上面的函数register_mokoid_server_LedService会把以C/C++实现的接口注册为java可调用的接口,比如mokoid_init为C/C++代码, 而_init则位java可以使用的接口。这个函数会在JNI_OnLoad里面被调用。 阅读更多下文请点击:http://www.hzlitai.com.cn/article/ARM11/SYSTEM/Android_HAL_kaifa2.html

评论