SensorService的啟動(dòng)是放在SystemServer的startBootstrapServices方法中温技,代碼如下:
frameworks/base/services/java/com/android/server/SystemServer.java
/**
* Start the sensor service. This is a blocking call and can take time.
*/
private static native void startSensorService();
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
...
mSensorServiceStart = SystemServerInitThreadPool.submit(() -> {
TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog();
traceLog.traceBegin(START_SENSOR_SERVICE);
startSensorService();
traceLog.traceEnd();
}, START_SENSOR_SERVICE);
}
在SystemServer的startBootstrapServices方法中調(diào)用startSensorService方法,而這個(gè)方法是JNI的native方法。那該方法是如何被添加到虛擬機(jī)的呢商膊?
frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();//SystemServer的入口函數(shù)卷中,調(diào)用了run方法
}
private void run() {
...
// Initialize native services.
System.loadLibrary("android_servers");//加載libandroid_servers.so文件
...
}
那這個(gè)so對(duì)應(yīng)的代碼是放在/frameworks/base/services/core/jni目錄下匿又,打開onload.cpp的JNI_OnLoad方法:
frameworks/base/services/core/jni/onload.cpp
extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("GetEnv failed!");
return result;
}
ALOG_ASSERT(env, "Could not retrieve the env!");
...
register_android_server_PowerManagerService(env);
register_android_server_InputManager(env);
register_android_server_AlarmManagerService(env);
...
register_android_server_SystemServer(env);//SensorService是放在SystemServer
...
}
在JNI_OnLoad方法中注冊(cè)了系統(tǒng)服務(wù)的JNI方法,如PowerManagerService滤祖、InputManager等等,我們的SensorService是放在SystemServer中的瓶籽,打開com_android_server_SystemServer.cpp匠童。
frameworks/base/services/core/jni/com_android_server_SystemServer.cpp
static void android_server_SystemServer_startSensorService(JNIEnv* /* env */, jobject /* clazz */) {
char propBuf[PROPERTY_VALUE_MAX];
property_get("system_init.startsensorservice", propBuf, "1");
if (strcmp(propBuf, "1") == 0) {
SensorService::publish(false /* allowIsolated */,
IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);//調(diào)用SersorService的publish方法
}
}
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"startSensorService", "()V", (void*)android_server_SystemServer_startSensorService},
{"startHidlServices", "()V", (void*)android_server_SystemServer_startHidlServices},
{"initZygoteChildHeapProfiling", "()V",
(void*)android_server_SystemServer_initZygoteChildHeapProfiling},
{"spawnFdLeakCheckThread", "()V",
(void*)android_server_SystemServer_spawnFdLeakCheckThread},
{"startIncrementalService", "()J",
(void*)android_server_SystemServer_startIncrementalService},
{"setIncrementalServiceSystemReady", "(J)V",
(void*)android_server_SystemServer_setIncrementalServiceSystemReady},
};
int register_android_server_SystemServer(JNIEnv* env)
{
return jniRegisterNativeMethods(env, "com/android/server/SystemServer",
gMethods, NELEM(gMethods));
}
從startSensorService->android_server_SystemServer_startSensorService->SensorService::publish,發(fā)現(xiàn)startSensorService調(diào)用的是SensorService的publish方法棘劣。我們來看下SensorService的定義:
frameworks/native/services/sensorservice/SensorService.h
class SensorService :
public BinderService<SensorService>,
public BnSensorServer,
protected Thread
SensorService是繼承BinderService的俏让,其中publish方法就是在BinderService中實(shí)現(xiàn)的。
template<typename SERVICE>
class BinderService
{
public:
static status_t publish(bool allowIsolated = false,
int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
sp<IServiceManager> sm(defaultServiceManager());//獲取ServiceManager對(duì)象
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,
dumpFlags);//調(diào)用addService添加服務(wù)
}
...
static void instantiate() { publish(); }
...
};
我們可以把SERVICE理解成JAVA中的泛型,SensorService繼承BinderService<SensorService>首昔,所以addService中的new SERVICE()就是調(diào)用SensorService的構(gòu)造方法得到實(shí)例化對(duì)象寡喝。
到此,我們的 SensorService就被啟動(dòng)啦@掌妗预鬓!