1.SystemServer.java
找到\frameworks\base\services\java\com\android\server\SystemServer
main方法
public static void main(String[] args) {
new SystemServer().run();
}
run方法中会调用startBootstrapServices() startCoreServices() startOtherServices(),我们需要看的是startOtherServices()
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()中,mActivityManagerService.systemReady 创建线程去执行startSystemUi(context)
private void startOtherServices() {
...
traceBeginAndSlog("StartSystemUI");
try {
startSystemUi(context, windowManagerF);
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
traceEnd();
...
}
startSystemUi中开启服务com.android.systemui.SystemUIService
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();
}
2.SystemUIService.java
@Override
public void onCreate() {
super.onCreate();
((SystemUIApplication) getApplication()).startServicesIfNeeded();
}
3.SystemUIApplication.java
SystemBars、StorageNotification、PowerUI都extends SystemUI,通过for循环调用start()方法,mServices[i].start(),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,//主要处理和Power相关的事件,比如省电模式切换、电池电量变化和开关屏事件等
RingtonePlayer.class,//铃声播放
KeyboardUI.class,//锁屏模块可以看做单独的应用,提供基本的手机个人隐私保护
PipUI.class,//提供对于画中画模式的管理
ShortcutKeyDispatcher.class,
VendorServices.class,
GarbageMonitor.Service.class,
LatencyTester.class,
GlobalActionsComponent.class,
};
public void startServicesIfNeeded() {
...
LogUtil.v(TAG, "Starting SystemUI services.");
final int N = SERVICES.length;
for (int i = 0; i < N; i++) {
Class<?> cl = SERVICES[i];
if (DEBUG)
LogUtil.d(TAG, "loading: " + cl);
try {
mServices[i] = (SystemUI) cl.newInstance();
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
}
mServices[i].mContext = this;
mServices[i].mComponents = mComponents;
if (DEBUG)
LogUtil.d(TAG, "running: " + mServices[i]);
mServices[i].start();
if (mBootCompleted) {
mServices[i].onBootCompleted();
}
}
...
}