本文
關機動作從按鍵觸發中斷,linux kernel層給android framework層返回按鍵事件進入 framework層,再從 framework層到kernel層執行kernel層關機任務。
長按鍵對應的handler代碼:
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
Runnable mPowerLongPress;
[java]
private final Runnable mPowerLongPress = new Runnable() {
public void run() {
if (!mPowerKeyHandled) {
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
}
}
};
private final Runnable mPowerLongPress = new Runnable() {
public void run() {
if (!mPowerKeyHandled) {
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
}
}
};mPowerLongPress 啟動關機對話方塊
(frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java)
如果我們選擇Power OFF’,會調用 ShutdownThread.shutdown. 啟動關機線程執行關機動作。
frameworks/base/core/java/com/android/internal/app/ShutdownThread.java
真正關機 流程:
(1)廣播全域事件, ACTION_SHUTDOWN Intent
(2)shutdown ActivityManager 服務
(3) 停止藍芽服務
(4) 停止 電話語音 (radio phone service)
(5)停止mount 服務
(6) 調用 Power.shutdown() 進入native 層
frameworks/base/core/java/android/os/Power.java
power的native實現代碼:
frameworks/base/core/jni/android_os_Power.cpp
[java]
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{
sync();
#ifdef HAVE_ANDROID_OS
reboot(RB_POWER_OFF);
#endif
}
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{
sync();
#ifdef HAVE_ANDROID_OS
reboot(RB_POWER_OFF);
#endif
}
sync, reboot 為linux系統調用,進入linux核心關機流程。