SystemUi概述:https://blog.csdn.net/xxdw1992/article/details/121351574
學習筆記
1、手機開機后,Android系統首先會創建一個Zygote(核心進程)。
2、由Zygote啟動SystemServer。
3、SystemServer會啟動系統運行所需的眾多核心服務和普通服務、以及一些應用及數據。例如:SystemUI 啟動就是從 SystemServer 里啟動的。
4、進入鎖屏界面,開機完成。
SystemServer 中有一個 main()方法為系統服務的入口;
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
在SystemServer 中的 main()方法中,就一句代碼生成 SystemServer 對象,執行run 方法。在run()方法里啟動了各類服務;
private void run() {
//省略部分代碼
// Start services.
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices(); // 在該方法里啟動了 SystemUI的服務。
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執行流程圖:
SystemUi進入到SystemUIService的onCreate()方法里;在onCreate()方法中獲得 SystemUIApplication 對象并調用其 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() 循環 start 了config_systemUIServiceComponents 里的 Service,這些服務不是四大組件之一的 Service, 而是繼承自 SystemUI 接口的服務,我們稱之為 SystemUI服務。
到此SystemUI 啟動流程分析完畢。