初識(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í)挺大的弹谁。但每份努力都是為了成就最后的自己吧,共勉!Tし摺沟于!