SystemUi概述:https://blog.csdn.net/xxdw1992/article/details/121351574
學(xué)習(xí)筆記
1、手機(jī)開(kāi)機(jī)后,Android系統(tǒng)首先會(huì)創(chuàng)建一個(gè)Zygote(核心進(jìn)程)。
2、由Zygote啟動(dòng)SystemServer。
3、SystemServer會(huì)啟動(dòng)系統(tǒng)運(yùn)行所需的眾多核心服務(wù)和普通服務(wù)、以及一些應(yīng)用及數(shù)據(jù)。例如:SystemUI 啟動(dòng)就是從 SystemServer 里啟動(dòng)的。
4、進(jìn)入鎖屏界面,開(kāi)機(jī)完成。
SystemServer 中有一個(gè) main()方法為系統(tǒng)服務(wù)的入口;
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
在SystemServer 中的 main()方法中,就一句代碼生成 SystemServer 對(duì)象,執(zhí)行run 方法。在run()方法里啟動(dòng)了各類(lèi)服務(wù);
private void run() {
//省略部分代碼
// Start services.
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices(); // 在該方法里啟動(dòng)了 SystemUI的服務(wù)。
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}
//省略部分代碼
}
private void startOtherServices() {
//省略部分代碼
t.traceBegin("StartSystemUI");
try {
startSystemUi(context, windowManagerF);
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
t.traceEnd();
//省略部分代碼
}
private static void startSystemUi(Context context, WindowManagerService windowManager) {
PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
Intent intent = new Intent();
intent.setComponent(pm.getSystemUiServiceComponent());
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.SYSTEM);
windowManager.onSystemUiStarted();
}
SystemServer執(zhí)行流程圖:
SystemUi進(jìn)入到SystemUIService的onCreate()方法里;在onCreate()方法中獲得 SystemUIApplication 對(duì)象并調(diào)用其 startServicesIfNeeded() 方法
@Override
public void onCreate() {
super.onCreate();
// Start all of SystemUI
((SystemUIApplication) getApplication()).startServicesIfNeeded();
// 省略部分代碼...
}
/**
* 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() {
String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
startServicesIfNeeded(names);
}
在SystemUIApplication中查看startServicesIfNeeded() 方法,其中其中 config_systemUIServiceComponents 值在frameworks/base/packages/SystemUI/res/values/config.xml 里:
private void startServicesIfNeeded(String[] services) {
if (mServicesStarted) {
return;
}
mServices = new SystemUI[services.length];
if (!mBootCompleted) {
// check to see if maybe it was already completed long before we began
// see ActivityManagerService.finishBooting()
if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
mBootCompleted = true;
if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
}
}
Log.v(TAG, "Starting SystemUI services for user " +
Process.myUserHandle().getIdentifier() + ".");
TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming",
Trace.TRACE_TAG_APP);
log.traceBegin("StartServices");
final int N = services.length;
for (int i = 0; i < N; i++) {
String clsName = services[i];
if (DEBUG) Log.d(TAG, "loading: " + clsName);
log.traceBegin("StartServices" + clsName);
long ti = System.currentTimeMillis();
Class cls;
try {
cls = Class.forName(clsName);
mServices[i] = (SystemUI) cls.newInstance();
} catch(ClassNotFoundException ex){
throw new RuntimeException(ex);
} 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();
//省略其他代碼
}
}
可以看到 startServicesIfNeeded() 循環(huán) start 了config_systemUIServiceComponents 里的 Service,這些服務(wù)不是四大組件之一的 Service, 而是繼承自 SystemUI 接口的服務(wù),我們稱(chēng)之為 SystemUI服務(wù)。
到此SystemUI 啟動(dòng)流程分析完畢。