SensorService之啟動(dòng)篇

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對應(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方法中注冊了系統(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對象
        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í)例化對象。

到此,我們的 SensorService就被啟動(dòng)啦??!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容