关机动作从按键触发中断,linux kernel层给android framework层返回按键事件进入 framework层,再从 framework层到kernel层执行kernel层关机任务。
长按键对应的handler代码:
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
Runnable mPowerLongPress;
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
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz) { sync(); #ifdef HAVE_ANDROID_OS reboot(RB_POWER_OFF); #endif }
sync, reboot 为linux系统调用,进入linux内核关机流程。