編輯:關於 Android 程式設計
日前遇到一個低機率重現的問題:鎖屏介面遲遲不顯示,畫面上只看得到狀態列。為了徹底釐清原因,我沿著鎖屏介面的啟動與顯示流程完整追蹤了一遍,在此整理分享,也方便日後自己查閱。先前已簡單介紹過 Zygote 的啟動流程,而當 Zygote 進程啟動後,會率先建立一個 SystemServer 進程,SystemServer 在呼叫 startOtherServices 的同時,也會一併呼叫 WindowManagerService 的 systemReady() 方法。
//frameworks/base/services/java/com/android/server/SystemServer.java private void startOtherServices() { ... wm = WindowManagerService.main(context, inputManager, mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL, !mFirstBoot, mOnlyCore); ... try { wm.systemReady(); Slog.i("jason11", "SystemServer wm.systemReady"); } catch (Throwable e) { reportWtf("making Window Manager Service ready", e); } ... }在 WindowManagerService 內部,則是直接轉手呼叫了 PhoneWindowManager 的 systemReady()。
//frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java //final WindowManagerPolicy mPolicy = new PhoneWindowManager(); public void systemReady() { mPolicy.systemReady(); }而在 PhoneWindowManager 的 systemReady() 中,會依據一個名為 bindKeyguardNow 的布林值,來決定是否要在此刻綁定 keyguard service。
//frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java /** {@inheritDoc} */ @Override public void systemReady() { mKeyguardDelegate = new KeyguardServiceDelegate(mContext); mKeyguardDelegate.onSystemReady(); readCameraLensCoverState(); updateUiMode(); boolean bindKeyguardNow; synchronized (mLock) { updateOrientationListenerLp(); mSystemReady = true; mHandler.post(new Runnable() { @Override public void run() { updateSettings(); } }); bindKeyguardNow = mDeferBindKeyguard; if (bindKeyguardNow) { // systemBooted ran but wasn't able to bind to the Keyguard, we'll do it now. mDeferBindKeyguard = false; } } if (bindKeyguardNow) { mKeyguardDelegate.bindService(mContext); mKeyguardDelegate.onBootCompleted(); } mSystemGestures.systemReady(); }看到這裡或許會聯想到:如果 bindKeyguardNow 為 false,不就代表不會綁定?繼續往下追蹤後發現,PhoneWindowManager 的 systemBooted() 裡同樣具備綁定 keyguard service 的邏輯。也就是說,只要在其中一處完成綁定,另一處便不會重複處理。就我實際測試的情況而言,是在 systemBooted 階段完成綁定的。
//frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java /** {@inheritDoc} */ @Override public void systemBooted() { boolean bindKeyguardNow = false; synchronized (mLock) { // Time to bind Keyguard; take care to only bind it once, either here if ready or // in systemReady if not. if (mKeyguardDelegate != null) { bindKeyguardNow = true; } else { // Because mKeyguardDelegate is null, we know that the synchronized block in // systemReady didn't run yet and setting this will actually have an effect. mDeferBindKeyguard = true; } } if (bindKeyguardNow) { mKeyguardDelegate.bindService(mContext); mKeyguardDelegate.onBootCompleted(); } synchronized (mLock) { mSystemBooted = true; } startedWakingUp(); screenTurningOn(null); screenTurnedOn(); }下面透過時序圖來說明 systemBooted 究竟是如何被觸發的,這裡就不再逐步追下去。

從上述分析可以得知,無論是在 systemReady 或 systemBooted,最終都會呼叫 KeyguardServiceDelegate 的 bindService 方法。接下來就以這個方法為起點,來看看鎖屏介面究竟是如何顯示出來的。先欣賞下面的時序圖,再來分步講解。

1、首先來看 KeyguardServiceDelegate 是如何綁定 KeyguardService 的。
//frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java public class KeyguardServiceDelegate { ... public void bindService(Context context) { Intent intent = new Intent(); final Resources resources = context.getApplicationContext().getResources(); final ComponentName keyguardComponent = ComponentName.unflattenFromString( resources.getString(com.android.internal.R.string.config_keyguardComponent)); intent.setComponent(keyguardComponent); if (!context.bindServiceAsUser(intent, mKeyguardConnection, Context.BIND_AUTO_CREATE, UserHandle.OWNER)) { Log.v(TAG, "*** Keyguard: can't bind to " + keyguardComponent); mKeyguardState.showing = false; mKeyguardState.showingAndNotOccluded = false; mKeyguardState.secure = false; synchronized (mKeyguardState) { // TODO: Fix synchronisation model in this class. The other state in this class // is at least self-healing but a race condition here can lead to the scrim being // stuck on keyguard-less devices. mKeyguardState.deviceHasKeyguard = false; hideScrim(); } } else { if (DEBUG) Log.v(TAG, "*** Keyguard started"); } } ... }在 bindService 中呼叫了 bindServiceAsUser 來綁定指定的 intent service,而 config_keyguardComponent 的定義如下。
//frameworks/base/core/res/res/values/config.xmlcom.android.systemui/com.android.systemui.keyguard.KeyguardService
當綁定成功之後,系統會回呼 mKeyguardConnection 裡的 onServiceConnected 方法。
//frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java public class KeyguardServiceDelegate { ... private final ServiceConnection mKeyguardConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { if (DEBUG) Log.v(TAG, "*** Keyguard connected (yay!)"); mKeyguardService = new KeyguardServiceWrapper(mContext, IKeyguardService.Stub.asInterface(service)); if (mKeyguardState.systemIsReady) { // If the system is ready, it means keyguard crashed and restarted. mKeyguardService.onSystemReady(); // This is used to hide the scrim once keyguard displays. if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE) { mKeyguardService.onStartedWakingUp(); } if (mKeyguardState.screenState == SCREEN_STATE_ON || mKeyguardState.screenState == SCREEN_STATE_TURNING_ON) { mKeyguardService.onScreenTurningOn( new KeyguardShowDelegate(mDrawnListenerWhenConnect)); } if (mKeyguardState.screenState == SCREEN_STATE_ON) { mKeyguardService.onScreenTurnedOn(); } mDrawnListenerWhenConnect = null; } if (mKeyguardState.bootCompleted) { mKeyguardService.onBootCompleted(); } if (mKeyguardState.occluded) { mKeyguardService.setOccluded(mKeyguardState.occluded); } } @Override public void onServiceDisconnected(ComponentName name) { if (DEBUG) Log.v(TAG, "*** Keyguard disconnected (boo!)"); mKeyguardService = null; } }; ... }
Android記憶體快取算法解析:從LRU、LFU到ImageLoader實作
前言 在Android圖片加載與資料快取場景中,記憶體快取扮演關鍵角色。適當的快取能減少重複加載,提高使用者體驗;但若策略不當,也可能造成記憶體壓力。本文以常見快取演算法
魅藍Note3如何啟用完整Root權限 補齊授權的方法
雖然不少同學已順利入手魅藍Note3,也照著網上教程拿到了Root權限,但使用部分修改類軟件時仍會提示「設備未取得Root權限」。這其實是因為魅藍Note3系統自帶的Ro
Android 仿 ZAKER 上推開門動畫實作(附 Demo 源碼)
用過 ZAKER 的人應該都記得那個開場:進入 App 後先看到一張大圖,你得往上滑到一定距離,那道「門」才會開啟,露出底下的主頁面。如果滑得不夠遠就放手,它還會帶著彈跳
ActivityGroup實戰:實現仿網易新聞頂部滑動導航欄
現在的應用要塞進去的內容越來越多,小屏幕上還想全部鋪開,於是頂部導航欄成了常見解法。但問題也很現實:Android 設備內存有限,若把大量界面全部緩存進內存,很容易觸發