Android核心程序之SystemUI - (一)开篇
UI是實現用戶交互的重要途徑之一,而Android中一個重要的UI元素就是SystemUI,本文分析基于Android 5.1,分析SystemUI的啟動及運行過程。
SystemUI源代碼所在路徑為:
frameworks/base/packages/SystemUI查看AndroidManifest.xml,整理軟件實現思路。
包名為com.android.systemui
package="com.android.systemui"分配的userID為android.uid.systemui
android:sharedUserId="android.uid.systemui"標明此應用為系統核心App
coreApp="true"接下來是一系列的權限聲明,包括Networking and telephony,Physical hardware,ActivityManager,WindowManager,DreamManager,Alarm clocks,Keyguard,Recents,Wifi Display,Screen Capturing等方面的權限。
接下來看 <application></application>中的內容:
此應用不會輕易被kill,并且會自動啟動
android:persistent="true"用戶數據不可清除
android:allowClearUserData="false"啟用硬件加速
android:hardwareAccelerated="true"設置系統進程
android:process="com.android.systemui"其他屬性為常規屬性,很多app都會有,在此不再贅述。
接下來是分析SystemUI的重點,看下都涉及到了哪些activity,service和receiver。
activity:
- .usb.UsbStorageActivity
- com.android.internal.app.ExternalMediaFormatActivity
- .recent.RecentsActivity
- .recents.RecentsActivity
- .usb.UsbConfirmActivity
- .usb.UsbPermissionActivity
- .usb.UsbResolverActivity
- .usb.UsbAccessoryUriActivity
- .usb.UsbDebuggingActivity
- .net.NetworkOverLimitActivity
- .media.MediaProjectionPermissionActivity
- .DessertCase
- .egg.LLandActivity
- .Somnambulator
- .settings.BrightnessDialog
service:
- SystemUIService
- .screenshot.TakeScreenshotService
- .LoadAverageService
- .ImageWallpaper
- .DessertCaseDream
- .keyguard.KeyguardService
- .doze.DozeService
receiver:
- .BootReceiver
- .qs.tiles.HotspotTile$APChangedReceiver
- .recent.RecentsPreloadReceiver
- .recents.RecentsUserEventProxyReceiver
眾多activity中并沒有LAUNCHER,也就是說SystemUI是沒有啟動界面的。
SystemUI的啟動
既然SystemUI沒有啟動界面,也無需人為啟動,那么SystemUI是怎么啟動起來的呢?接下來就將對這個問題進行說明。
- 在service中有一個SystemUIService,這就是入口。那么這個SystemUIService又是怎么啟動的呢?要找到答案,就要考慮到這是一個系統app,自然會在系統完成初始化以后啟動,那么利用Source Insight在frameworks/base中看看有沒有。
從結果來看,在SystemService貌似有什么線索。打開SystemService.java(frameworks/base/services/java/com/android/server/),以下代碼就展現在眼前了。
static final void startSystemUi(Context context) {Intent intent = new Intent();intent.setComponent(new ComponentName("com.android.systemui","com.android.systemui.SystemUIService"));//Slog.d(TAG, "Starting service: " + intent);context.startServiceAsUser(intent, UserHandle.OWNER); }在代碼中可以看到SystemUIService被啟動了起來,那么哪里調用了這個方法呢?在此類中繼續查找,又發現如下代碼:
mActivityManagerService.systemReady(new Runnable() {@Overridepublic void run() {Slog.i(TAG, "Making services ready");mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);try {mActivityManagerService.startObservingNativeCrashes();} catch (Throwable e) {reportWtf("observing native crashes", e);}Slog.i(TAG, "WebViewFactory preparation");WebViewFactory.prepareWebViewInSystemServer();try {startSystemUi(context);} catch (Throwable e) {reportWtf("starting System UI", e);}...} }由代碼可知,ActivityManagerService調用了systemReady()方法,也就是系統就緒就會調用run()方法。那么會在何時執行到此處呢?再往上探索,發現上述代碼位于startOtherServices()方法中,那么還是那個問題,何時調用startOtherServices()方法。再往上探索,發現startOtherServices()位于一個run()方法中
private void run() {···// Start services.try {startBootstrapServices();startCoreServices();startOtherServices();} catch (Throwable ex) {Slog.e("System", "******************************************");Slog.e("System", "************ Failure starting system services", ex);throw ex;}··· }再往上追一追,最終找到了這個類的開始:
public static void main(String[] args) {new SystemServer().run(); }嗯,這是···,很熟悉嘛。
至于SystemService則是由Zygote啟動的,在此就不多追溯了。
至此,SystemServer已經啟動,那么在SystemServer里面又做了什么事情呢?
SystemUIService的初始化
在SystemUIService中,主要代碼就一個onCreate()方法,來看看他做了啥:
@Override public void onCreate() {super.onCreate();((SystemUIApplication) getApplication()).startServicesIfNeeded(); }轉型為SystemUIApplication并且調用startServicesIfNeeded()方法。那么接下來去看看startServicesIfNeeded()方法做了些什么。
public void startServicesIfNeeded() {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.");final int N = SERVICES.length;for (int i=0; i<N; i++) {Class<?> cl = SERVICES[i];if (DEBUG) Log.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) Log.d(TAG, "running: " + mServices[i]);mServices[i].start();if (mBootCompleted) {mServices[i].onBootCompleted();}}mServicesStarted = true; }根據代碼中涉及到的mServices,追溯其來源:
private final SystemUI[] mServices = new SystemUI[SERVICES.length];
再往上,得到SERVICES的由來:
在這里定義了很多System Panel,這里叫做SERVICES,并非真正的services,運行在SystemUIService中的子服務。此時再來回頭看onCreate()方法:
@Override public void onCreate() {super.onCreate();// Set the application theme that is inherited by all services. Note that setting the// application theme in the manifest does only work for activities. Keep this in sync with// the theme set there.setTheme(R.style.systemui_theme);IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (mBootCompleted) return;if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");unregisterReceiver(this);mBootCompleted = true;if (mServicesStarted) {final int N = mServices.length;for (int i = 0; i < N; i++) {mServices[i].onBootCompleted();}}}}, filter); }在onCreate()方法中,注冊了廣播接收器,用于接收開機完成的廣播,并將開機完成的狀態傳遞給每個SERVICE。
經過以上代碼的追溯,可知:
mServices[i] = (SystemUI)cl.newInstance();//實例化子服務,并將其存儲于mService[i]中。
mServices[i].mContext = this; //設置mService[i]的Context
mServices[i].mComponents = mComponents;//設置mService[i]的Components
mServices[i].start();//運行mService[i]
至此,SystemUIService的啟動就已完成,伴隨著SystemUIService的完成,SystemUI的核心service也就啟動了,SystemUIService的使命也就完成了,接下來的事情就交由各個子服務去完成了。
以上屬于個人體驗心得總結,若有不足之處,還望不吝賜教,歡迎批評指正,共同進步
轉載于:https://www.cnblogs.com/cj5785/p/9892979.html
總結
以上是生活随笔為你收集整理的Android核心程序之SystemUI - (一)开篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux之SQL语句简明教程---SU
- 下一篇: android sina oauth2.