1. Power.java--> find corresponding native cfile(查找对应的具体用C实现的C文件)
android.os.Power.java -------- native file ---->.../jni/android_os_Power.c
2. in android_os_Power.c, you canfind:
static JNINativeMethod method_table[]= // Native functiontable{
{"acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock},
{"releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock},
{"setLastUserActivityTimeout", "(J)I",(void*)setLastUserActivityTimeout },
{"setScreenState", "(Z)I", (void*)setScreenState },
{"shutdown", "()V", (void*)android_os_Power_shutdown },
{ "reboot","(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
};int register_android_os_Power(JNIEnv *env)// function to register mapping tablefrom name to function
{
returnAndroidRuntime::registerNativeMethods(
env, "android/os/Power",
method_table, NELEM(method_table));
}
3. in /framework/base/core/jni , a file named:AndroidRuntime.cpp
3.1) a global register function array
static const RegJNIRec gRegJNI[] =
{
...
register_android_os_Power,
}
3.2) Register native function process
int AndroidRuntime::startReg(JNIEnv* env)
or
Java_com_android_internal_util_WithFramework_registerNatives(...)
or
Java_LoadClass_registerNatives(....)
---> register_jni_procs(gRegJNI, NELEM(gRegJNI),env)
---> foreach(member of gRegJNI) call
register_XXX_XXX_XXX..XXX() //so here register_android_os_power() will becalled
--->
AndroidRuntime::registerNativeMethods(env, class_namelike "android/os/Power", method table like method_table,size)
---> jniRegisterNativeMethods(env, className,gMethods, numMethods)
-->pEnv->RegisterNatives(env, clazz, gMethods,numMethods) ;
--> foreach(method) calldvmRegisterJNIMethod(ClassObject* clazz, const char*methodName,
constchar* signature, void* fnPtr)
--> calldvmSetNativeFunc(method, dvmCallSynchronizedJNIMethod, fnPtr); //for sycn method
or
call dvmSetNativeFunc(method, dvmCallJNIMethod,fnPtr);
--> ((Method*)method)->insns = insns; // set actual codespace to be executed for a native function
4.calling a native method ( JNI method)
void dvmPlatformInvoke(void* pEnv,ClassObject* clazz, int argInfo, int argc,
const u4*argv, const char* shorty, void* func, JValue*pReturn)
const u4*argv, const char* shorty, void* func, JValue*pReturn)
dvmCallMethod() /dvmInvokeMethod
---> if(dvmIsNativeMethod(method))
{
(*method->nativeFunc)(self->curFrame,&retval, method, self);
}
(*method->nativeFunc)(self->curFrame,&retval, method, self);
}