安卓用power键关闭并重新启动的过程(安卓层) ) ) )。
一、结论是安卓系统的关闭和重启最终是通过修改SystemProperties的属性来完成的。
二.属性名称
关闭: name:sys.powerctlvalue:shutdownreasonreason可以为空; 因此,系统属性. set (sys.power CTL )、' shutdown,'可以完全用于三方APP
利索恩; 关机; 同事也在ADB shell setprop上
在sys.powerctl shutdown中关闭。
重新启动: name:sys.powerctlvalue:reboot 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/global actions.Java
代码比较简单,不是本文的重点。 我不贴这个。
四.代码流分析:
1、首先,GlobalActions有onPress ()和onLongPress () )的处理,很简单。 对应的是关机和重新启动。
//切断电源
公共void onpress (
//shutdownbymakingsureradioandpowerarehandledaccordingly。
mwindowmanagerfuncs.shut down (false/* confirm */);
}
}
//重启
公共布尔onlongpress (
usermanagerum=(usermanager ) mcontext.getsystemservice ) context.user_service;
if (! um.hasuserrestriction (user manager.disallow _ safe _ boot ) }
mwindowmanagerfuncs.reboot safe mode (true;
返回真;
}
返回假;
}
2、为了便于说明,以关机流程为例进行说明,类似重启。 可以在代码中直接调用启用mWindowManagerFuncs的方法。 mWindowManagerFuncs是抽象类的WindowManagerFuncs对象,WindowManagerPolicy是WindowManagerPolicy内部的抽象内部类,最终由WindowManagerService
公共语音关闭(布尔型确认)。
shut down thread.shut down (m context,power manager.shut down _ user _ requested,confirm );
}
什么也别说,代码的重点是一句话,直接进入ShutdownThread
3、进入ShutdownThread的方法,看具体做了什么处理
publicstaticvoidshutdown (finalcontextcontext,String reason,boolean confirm ) {
//省略时
/接下来这是注意事项。 那么,为什么在跑monkey的时候不能手动关闭引擎呢? 原
//因为在这里,所以在monkey的时候直接返回的话。 请注意这不关你的事
//无法删除设备。 只是,不能用键删除。 你也可以用开头的方法切断电源。
系统属性. get布尔(' ro.monkey ',false ) )。
log.d(tag,' cannotrequesttoshutdownwhenmonkeyisrunning,returning.' );
返回;
}
//真正关机的重点流程就在这里
shutdowninner(co
ntext, 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;
}
}