SystemServer 启动SensorService
Zygote启动之后,调用SystemServer的main方法(调用run方法)启动系统服务;
代码路径:
./frameworks/base/services/java/com/android/server/SystemServer.java
SystemServer类中提供的run 方法中,在启动service之前,会加载本地动态库System.loadLibrary(“android_servers”)初始化本地 Native service,过程如下:
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
public final class SystemServer {
private static final String TAG = "SystemServer";
private void run() {
// Initialize native services.
System.loadLibrary("android_servers");
// Start services.
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
}
...
}
在 startBootstrapServices(); 的过程的最后 会调用startSensorService(); 如下:
private void startBootstrapServices() {
......
// The sensor service needs access to package manager service, app ops
// service, and permissions service, therefore we start it after them.
startSensorService();
}
在SystemServer类定义中,定义了本地方法startSensorService 如下:
public final class SystemServer {
private static final String TAG = "SystemServer";
//......
private static native void startSensorService();
//......
}
当SystemServer调用run 方法,会通过JNI调用到本地方法;
JNI 访问native 方法
通过对JNI的了解 System.loadLibrary(“android_servers”); 会去查找libandroid_servers.so 这个库文件;
:~/prj/SC20_R06_master_0526/code/frameworks$ grep -rn libandroid_servers ./
./base/services/Android.mk:54:LOCAL_MODULE:= libandroid_servers
通过检索可知,加载该本地库,会调用在 base/services/ 下编译库文件的onload函数;即:
代码路径: ./base/services/core