Android 關機流程

To power off an Android phone, keep pressing power bottom then shutdown menu is appeard. Then choose ‘Power OFF’ to power off actually.
I looked for the source code to see how to do these sequence.

Showing shutdown menu

Long press of power bottom is handled here.
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
Runnable mPowerLongPress;

Here is the shutdown dialog.
frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java
If you choose ‘Power OFF’, it calls ShutdownThread.shutdown.

Shutdown process

frameworks/base/core/java/com/android/internal/app/ShutdownThread.java
Through beginShutdownSequence(), actual shutdown process is done in run().

  1. Broadcasts ACTION_SHUTDOWN Intent

  2. Calls shutdown of ActivityManager service

  3. Calls disable of Bluetooth service

  4. Calls Radio(false) of Phone service

  5. Calls shutdown of Mount service

  6. Vibrate for a while

  7. Calls Power.shutdown();

補充:

按下power鍵,gpio驅動會上報按鍵消息,EventHub會將消息進行分發。

在處理power鍵的長按短按上,有些很巧妙的地方,看下面的代碼

private void interceptPowerKeyDown(boolean handled) {

        mPowerKeyHandled = handled;

        if (!handled) {

            mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());

        }

    }

postDelayed 設置了500ms的延時

 private final Runnable mPowerLongPress = new Runnable() {

        public void run() {

            if (!mPowerKeyHandled) {

                mPowerKeyHandled = true;

                。。。。。

            }

        }

    };

看看Runnable,通過判斷mPowerKeyHandled的狀態判斷是否要執行,顯然,在button up的地方也會對mPowerKeyHandled進行設置。因此,如果在500ms之前,mPowerLongPress還沒有執行之前鬆手,就改變mPowerKeyHandled的狀態。這樣mPowerKeyHandled就不執行相關的功能了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章