Android 通过power键关机重启的流程(Android层)
一、结论,android系统的关机和重启最终都是通过修改SystemProperties的属性来完成的。
二、属性名称
关机:name:sys.powerctl value:shutdown+reason reason可以为空;所以完全可以在三方应用中通过SystemProperties.set("sys.powerctl", "shutdown,"
+ reason);来进行关机;同事你也可以通过adb shell setprop
sys.powerctl shutdown来进行关机。
重启:name:sys.powerctl value:reboot+reason 同样reason可以为空;如果你需要重启到recovery,那么value对应的为reboot,recovery。
三、说明:长按的关机和重启是在如下几个xml中进行声明定义,完了之后在GlobalActions中进行对应的处理
frameworks/base/core/res/res/values/config.xm
frameworks/base/core/res/res/values/strings.xml
frameworks/base/core/res/res/values-zh-rCN/strings.xml
frameworks/base/policy/src/com/Android/internal/policy/impl/GlobalActions.Java
代码比较简单,不是本文的重点。这就不贴了。
四、代码流程分析:
1、首先在GlobalActions中有处理onPress()和onLongPress(),很简单,对应的就是关机和重启;
//关机
public void onPress() {
// shutdown by making sure radio and power are handled accordingly.
mWindowManagerFuncs.shutdown(false /* confirm */);
}
}
//重启
public boolean onLongPress() {
UserManager um = (UserManager)mContext.getSystemService(Context.USER_SERVICE);
if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
mWindowManagerFuncs.rebootSafeMode(true);
return true;
}
return false;
}
2、为了方便说明,以关机流程为例进行说明,重启的类似。可以看到在代码中直接调用mWindowManagerFuncs对应的方法。mWindowManagerFuncs是抽象类WindowManagerFuncs的对象,WindowManagerFuncs是WindowManagerPolicy内部一个抽象内部类,最终是由WindowManagerService来实现的,所以理所当然的接下来代码会进入到WindowManagerService中的shutdown方法中;
public void shutdown(boolean confirm) {
ShutdownThread.shutdown(mContext, PowerManager.SHUTDOWN_USER_REQUESTED,confirm);
}
没什么好说的,代码关键点就一句,直接进入ShutdownThread
3、进入ShutdownThread的方法中,看具体做了什么处理
public static void shutdown(final Context context, String reason,boolean confirm) {
//省略如果
//接下来这个是一个注意点,那么就是为什么再跑monkey时手动关机关不了。原
//因就在这里,可以看到如果在monkey的时候直接return了。注意,这并不是说你
//无法关掉设备,只是无法通过按键关,你同样可以采用文章开头的方式进行关机。
if (SystemProperties.getBoolean("ro.monkey", false)) {
Log.d(TAG, "Cannot request to shutdown when Monkey is running,returning.");
return;
}
//真正关机的重点流程在这
shutdownInner(context, confirm);
}
4、shutdownInner的内部处理
static void shutdownInner(final Contextcontext, boolean confirm) {
//省略
//在这部分代码中会构造处确定是否关机的dialog,并处理起对应的事件,篇幅过长
//这里就省略了。关机对应的处理在下面这个方法
beginShutdownSequence(context);
}
private static void
beginShutdownSequence(Context context) {
// 省略
// sInstance是shutdownThread的实例,shutdownThread是一个Thread,所以接下来run()
sInstance.start();
}
5、shutdownThread线程的处理
public void run() {
checkShutdownFlow();
while (mShutdownFlow == IPO_SHUTDOWN_FLOW) {
mShutdownManager.saveStates(mContext);
mShutdownManager.enterShutdown(mContext);
switchToLauncher();
running();
}
if (mShutdownFlow != IPO_SHUTDOWN_FLOW) {
mShutdownManager.enterShutdown(mContext);
switchToLauncher();
running();
}
}
6、
private void running() {
//省略
/*
* Write a system property in case the system_server reboots before we
* get to the actual hardware restart. If that happens, we'll retry at
* the beginning of the SystemServer startup.
*/
{
String reason = (mReboot ? "1" : "0") + (mReason !=null ? mReason : "");
SystemProperties.set(SHUTDOWN_ACTION_PROPERTY, reason);
}
/*
* If we are rebooting into safe mode, write a system property
* indicating so.
*/
if (mRebootSafeMode) {
SystemProperties.set(REBOOT_SAFEMODE_PROPERTY, "1");
}
//关机的广播在这里发出去的
/// M:2012-05-20ALPS00286063 @{
mContext.sendBroadcast(new Intent(ACTION_PRE_SHUTDOWN));
/// @}2012-05-20
Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
intent.putExtra("_mode", mShutdownFlow);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
mContext.sendOrderedBroadcastAsUser(intent,
UserHandle.ALL, null, br, mHandler, 0, null, null);
//关闭radio,包括蓝牙wifi telephony等
// Shutdown radios.
Log.i(TAG, "Shutting down radios...");
shutdownRadios(MAX_RADIO_WAIT_TIME);
if (mRebootHasProgressBar) {
sInstance.setRebootProgress(RADIO_STOP_PERCENT, null);
}
//…………
rebootOrShutdown(mContext, mReboot, mReason);
}
}
7、在rebootOrShutdown这个方法中直接调用
PowerManagerService.lowLevelShutdown(reason);
完了之后在lowLevelShutdown中直接通过SystemProperties.set("sys.powerctl", "shutdown,"
+ reason);关机。
往下之后的处理会进入到kernel进行。后续在整理。
五、另外,上面说的是按power键弹dialog关机重启的流程,关于一直长按power键关机是在PhoneWindowManager中调用powerLongPress处理的。
private void powerLongPress() {
final int behavior = getResolvedLongPressOnPowerBehavior();
switch (behavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS,false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
showGlobalActionsInternal();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS,false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(behavior ==LONG_PRESS_POWER_SHUT_OFF);
//原理一样,也是调用 mWindowManagerFuncs.shutdown方法
break;
}
}