Android8.1 SystemUI啟動(dòng)流程

初識(shí)SystemUI

SystemUI是為用戶提供的系統(tǒng)級(jí)別的信息顯示與交互的一套UI組件,盡管它的表現(xiàn)形式與普通Android應(yīng)用程序大相徑庭,但它卻是以一個(gè)apk的其實(shí)存在于系統(tǒng)之中,即它與普通android應(yīng)用程序并沒有本質(zhì)上的區(qū)別。它也是通過Android四大組件來接受外界的請(qǐng)求并執(zhí)行相關(guān)操作。

SystemUI啟動(dòng)流程

1.frameworks/base/services/java/com/android/server/SystemServer.java(它是在ZygoteInit中進(jìn)行創(chuàng)建,并且啟動(dòng)起來的)

 /**
 * The main entry point from zygote.
 */
public static void main(String[] args) {
    new SystemServer().run();
}

接下來,我們看run方法,

 private void run() {
   ...

    // Start services.
    try {
        traceBeginAndSlog("StartServices");
        startBootstrapServices();
        startCoreServices();
        startOtherServices();
        SystemServerInitThreadPool.shutdown();
    } catch (Throwable ex) {
        Slog.e("System", "******************************************");
        Slog.e("System", "************ Failure starting system services", ex);
        throw ex;
    } finally {
        traceEnd();
    }
   ...
}

注意上述方法中的startOtherServices()方法,

/**
 * Starts a miscellaneous grab bag of stuff that has yet to be refactored
 * and organized.
 */
private void startOtherServices() {
     ...
     ...
  try {
            startSystemUi(context, windowManagerF);
        } catch (Throwable e) {
            reportWtf("starting System UI", e);
        }
     ...
     ...
  }

接著看StartSystemUi方法,

static final void startSystemUi(Context context, WindowManagerService windowManager) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.android.systemui",
                "com.android.systemui.SystemUIService"));
    intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
    //Slog.d(TAG, "Starting service: " + intent);
    context.startServiceAsUser(intent, UserHandle.SYSTEM);
    windowManager.onSystemUiStarted();
}

以上完成了SystemUIService的啟動(dòng)過程。

2.frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
SystemUIService繼承與Service,首先看重寫的onCreate方法,

 @Override
public void onCreate() {
    super.onCreate();
    ((SystemUIApplication) getApplication()).startServicesIfNeeded();

    // For debugging RescueParty
    if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("debug.crash_sysui", false)) {
        throw new RuntimeException();
    }
}

它調(diào)用了SystemUIApplication的startServicesIfNeeded()方法,接下來我們看SystemUIApplication中的startServicesIfNeeded()方法
3.frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java

/**
 * Makes sure that all the SystemUI services are running. If they are already running, this is 
  a
 * no-op. This is needed to conditinally start all the services, as we only need to have it in
 * the main process.
 * <p>This method must only be called from the main thread.</p>
 */

public void startServicesIfNeeded() {
    startServicesIfNeeded(SERVICES);
}

接著往下跟,

private void startServicesIfNeeded(Class<?>[] services) {
    if (mServicesStarted) {
        return;
    }
     ......
     ......
    final int N = services.length;
    for (int i = 0; i < N; i++) {
        Class<?> cl = services[i];
        if (DEBUG) Log.d(TAG, "loading: " + cl);
        log.traceBegin("StartServices" + cl.getSimpleName());
        long ti = System.currentTimeMillis();
        try {

            Object newService = SystemUIFactory.getInstance().createInstance(cl);
            mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        }

        mServices[i].mContext = this;
        mServices[i].mComponents = mComponents;
        if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
        mServices[i].start();
        log.traceEnd();

        // Warn if initialization of component takes too long
        ti = System.currentTimeMillis() - ti;
        if (ti > 1000) {
            Log.w(TAG, "Initialization of " + cl.getName() + " took " + ti + " ms");
        }
        if (mBootCompleted) {
            mServices[i].onBootCompleted();
        }
    }
   ...
}

可以看到,上述代碼中,有一個(gè)for循環(huán)的遍歷,那么這個(gè)service[i]是什么呢?我們可以找到代碼中初始化的地方,

/**
 * The classes of the stuff to start.
 */
private final Class<?>[] SERVICES = new Class[] {
        Dependency.class,
        NotificationChannels.class,
        CommandQueue.CommandQueueStart.class,
        KeyguardViewMediator.class,
        Recents.class,
        VolumeUI.class,
        Divider.class,
        SystemBars.class,
        StorageNotification.class,
        PowerUI.class,
        RingtonePlayer.class,
        KeyboardUI.class,
        PipUI.class,
        ShortcutKeyDispatcher.class,
        VendorServices.class,
        GarbageMonitor.Service.class,
        LatencyTester.class,
        GlobalActionsComponent.class,
        RoundedCorners.class,
};

這里是拿到每個(gè)和 SystemUI 相關(guān)的類的反射,存到了 service[] 里,然后賦值給cl,緊接著將通過反射將其轉(zhuǎn)化為具體類的對(duì)象,存到了mService[i]數(shù)組里,最后對(duì)象調(diào) start() 方法啟動(dòng)相關(guān)類的服務(wù),啟動(dòng)完成后,回調(diào) onBootCompleted( ) 方法。
mService[i] 里的值不同時(shí),調(diào)用的 start() 方法也不相同。
以上就是SystemUI啟動(dòng)的大致流程,具體的對(duì)應(yīng)的每個(gè)不同的會(huì)在后續(xù)做詳細(xì)的分析。

ps:其實(shí)接觸開發(fā)的時(shí)間并不久,而framework更是剛接觸幾天,壓力其實(shí)挺大的。但每份努力都是為了成就最后的自己吧,共勉?。?!

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

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評(píng)論 25 709
  • Android系統(tǒng)源代碼情景分析筆記 Activity組件的啟動(dòng)過程分析 [toc] 根Activity啟動(dòng)的過程...
    一只胖Wa牛閱讀 2,294評(píng)論 0 6
  • 杏白桃紅雪如梨,含羞不語(yǔ)正可期。 東風(fēng)壓倒西風(fēng)日,自是人間晴暖時(shí)。
    梅心梅飛閱讀 343評(píng)論 7 24
  • G189 -1組 谷琪宇 我是G189期1組的谷琪宇,很高興迎著清晨的陽(yáng)光和大家分享。先介紹一下我的三個(gè)標(biāo)簽。...
    GokGiU閱讀 242評(píng)論 0 1
  • 文/寧汐染 那年盛夏 薔薇花爬滿了圍墻 銀杏樹青蔥翠綠 香樟樹下的我們笑靨如花 那年盛夏 老舊的風(fēng)扇發(fā)出“吱呀”的...
    寧汐染閱讀 515評(píng)論 2 8

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