最近在學習并接觸SystemUI模塊的工作评汰,Keyguard做為引入庫也納入了SystemUI的大家庭,所以整體感覺SystemUI模塊還是挺大的哭懈,為了日后的查閱還是要寫一下筆記凿渊。筆記記錄過程中參考了許多網友的文章笼呆,在本文最后的文章參考會一一羅列带到,在此非常感謝他們的分享昧碉。
SystemUI屬于系統(tǒng)級的apk,位置在frameworks\base\packages\SystemUI阴孟,主要功能有:
狀態(tài)欄信息顯示,比如電池税迷,wifi信號,3G/4G等icon顯示
通知面板箭养,比如系統(tǒng)消息慕嚷,第三方應用消息
近期任務欄顯示面板,比如長按近期任務快捷鍵毕泌,顯示近期使用的應用
截圖服務
壁紙服務
……
SystemServer啟動后喝检,會在Main Thread啟動ActivityManagerService,當ActivityManagerService systemReady后撼泛,會去啟動SystemUIService挠说。?
SystemServer路徑:/base/services/java/com/android/server/SystemServer.java
mActivityManagerService.systemReady(newRunnable() {@Overridepublicvoidrun() {? ? ? ? ? ? ? ? Slog.i(TAG,"Making services ready");? ? ? ? ? ? ? ? ......? ? ? ? ? ? ? ? Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,"StartSystemUI");try{? ? ? ? ? ? ? ? ? ? startSystemUi(context);? ? ? ? ? ? ? ? }catch(Throwable e) {? ? ? ? ? ? ? ? ? ? reportWtf("starting System UI", e);? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);? ? ? ? ? ? ? ? ......? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在這個方法里啟動一個SystemUIService服務,
static final void startSystemUi(Context context) {? ? ? ? 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);}
1
2
3
4
5
6
7
8
通過startServiceAsUser愿题,SystemUIService就啟動了损俭,即SystemUI進程開機啟動。
publicclassSystemUIServiceextendsService{@OverridepublicvoidonCreate() {super.onCreate();? ? ? ? ((SystemUIApplication) getApplication()).startServicesIfNeeded();? ? }? ? ......
1
2
3
4
5
6
7
8
在SystemUIService的onCreate方法中會調用SystemUIApplication的startServicesIfNeeded方法潘酗,這個方法會調用 startServicesIfNeeded(SERVICES)方法啟動一系列服務(并不是真正的service,都繼承自SystemUI)杆兵。
public class SystemUIApplication extends Application {? ? ....../**
? ? * The classes of the stuff to start.
? ? */private final Class[] SERVICES = new Class[] {com.android.systemui.tuner.TunerService.class,com.android.systemui.keyguard.KeyguardViewMediator.class,com.android.systemui.recents.Recents.class,com.android.systemui.volume.VolumeUI.class,? ? ? ? ? ? Divider.class,com.android.systemui.statusbar.SystemBars.class,com.android.systemui.usb.StorageNotification.class,com.android.systemui.power.PowerUI.class,com.android.systemui.media.RingtonePlayer.class,com.android.systemui.keyboard.KeyboardUI.class,com.android.systemui.tv.pip.PipUI.class,com.android.systemui.shortcut.ShortcutKeyDispatcher.class,com.android.systemui.VendorServices.class};......? ? public void startServicesIfNeeded() {? ? ? ? startServicesIfNeeded(SERVICES);}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
startServicesIfNeeded方法會遍歷SERVICES 這個數組,依次調用service的start方法啟動服務。
privatevoidstartServicesIfNeeded(Class[] services) {if(mServicesStarted) {return;? ? ? ? }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() +".");? ? ? ? finalintN = services.length;for(inti=0; i cl = services[i];if(DEBUG) Log.d(TAG,"loading: "+ cl);try{? ? ? ? ? ? ? ? Object newService = SystemUIFactory.getInstance().createInstance(cl);? ? ? ? ? ? ? ? mServices[i] = (SystemUI) ((newService ==null) ? cl.newInstance() : newService);? ? ? ? ? ? }catch(IllegalAccessException ex) {thrownewRuntimeException(ex);? ? ? ? ? ? }catch(InstantiationException ex) {thrownewRuntimeException(ex);? ? ? ? ? ? }? ? ? ? ? ? mServices[i].mContext =this;? ? ? ? ? ? mServices[i].mComponents = mComponents;if(DEBUG) Log.d(TAG,"running: "+ mServices[i]);? ? ? ? ? ? mServices[i].start();if(mBootCompleted) {? ? ? ? ? ? ? ? mServices[i].onBootCompleted();? ? ? ? ? ? }? ? ? ? }? ? ? ? mServicesStarted =true;? ? }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
看到這里仔夺,這么多文字可能不夠直觀琐脏,那就看看圖吧,非常感謝參考文章中的分享。?
這里以SERVICES 中com.android.systemui.keyguard.KeyguardViewMediator.class為例日裙,?
SystemUI Services啟動后吹艇,根據各Services的功能,SystemUI的各個模塊就開始正常工作起來了阅签。