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().
-
Broadcasts ACTION_SHUTDOWN Intent
-
Calls shutdown of ActivityManager service
-
Calls disable of Bluetooth service
-
Calls Radio(false) of Phone service
-
Calls shutdown of Mount service
-
Vibrate for a while
-
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就不執行相關的功能了。