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對(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)啦@掌妗预鬓!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市赊颠,隨后出現(xiàn)的幾起案子格二,更是在濱河造成了極大的恐慌,老刑警劉巖竣蹦,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件顶猜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡痘括,警方通過查閱死者的電腦和手機(jī)长窄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纲菌,“玉大人挠日,你說我怎么就攤上這事『采啵” “怎么了嚣潜?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長椅贱。 經(jīng)常有香客問我懂算,道長,這世上最難降的妖魔是什么夜涕? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任犯犁,我火速辦了婚禮,結(jié)果婚禮上女器,老公的妹妹穿的比我還像新娘酸役。我一直安慰自己,他們只是感情好驾胆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布涣澡。 她就那樣靜靜地躺著,像睡著了一般丧诺。 火紅的嫁衣襯著肌膚如雪入桂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天驳阎,我揣著相機(jī)與錄音抗愁,去河邊找鬼馁蒂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蜘腌,可吹牛的內(nèi)容都是我干的沫屡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼撮珠,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼沮脖!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起芯急,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤勺届,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后娶耍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體免姿,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年榕酒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了养泡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奈应,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出购披,到底是詐尸還是另有隱情杖挣,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布刚陡,位于F島的核電站惩妇,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏筐乳。R本人自食惡果不足惜歌殃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蝙云。 院中可真熱鬧氓皱,春花似錦、人聲如沸勃刨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽身隐。三九已至廷区,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贾铝,已是汗流浹背隙轻。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來泰國打工埠帕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人玖绿。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓敛瓷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親镰矿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子琐驴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容