深入解析Android关机

来源:互联网 发布:python csv文件 求和 编辑:程序博客网 时间:2024/06/05 15:00


Android 关机顺序

  • 当我们长按电源按钮时,手机里究竟发了什么?
  • 什么又是关机顺序?
  • Android的关机顺序与Linux桌面系统有何不同
  • 如何更改关机菜单?

当我们说起Android的关机顺序时,许多诸如此类的问题便会涌进我们的脑袋。 不过,在继续阅读之前,建议您首先能对开机顺序有一个了解开机顺序文章。

Android是基于Linux内核的开源操作系统。尽管x86(x86 是一系列计算机微处理器指令集及其架构的统称,这种架构基于Intel 8086 CPU)是大多数Linux系统所采用的处理器架构,然而绝大多数Android系统却运行于ARM架构之上(ARM,又称Advanced RISC Machine,其前身为Acorn RISC Machine),除了来自IntelXolo设备。这种移动设备使用了Atom 1. 6Ghz x86处理器。 但不论哪种架构,Android的关机顺序都区别于Linux的桌面系统,如Ubuntu、Fedora。 本文主要介绍Android的关机顺序, 如果想更多地了解Linux桌面系统的关机顺序,请参考Linux的启动与关闭流程

下图详细阐释了Android的关机顺序

第一步按住电源按钮半秒钟(500ms)。

第二步之后,PhoneWindowManager.java 将捕获长按电源按钮这一事件并调用“interceptKeyBeforeQueueing”方法

下面是处理长按电源键事件的代码片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** {@inheritDoc} */
@Override
publicint interceptKeyBeforeQueueing(KeyEvent event, intpolicyFlags, booleanisScreenOn) {
....
....
....
caseKeyEvent.KEYCODE_POWER: {
     result &= ~ACTION_PASS_TO_USER;
       if(down) {
         if(isScreenOn && !mPowerKeyTriggered
               && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                   mPowerKeyTriggered = true;
                   mPowerKeyTime = event.getDownTime();
                   interceptScreenshotChord();
            }
               ITelephony telephonyService = getTelephonyService();
                booleanhungUp = false;
               if(telephonyService != null) {
                   try{
                       if(telephonyService.isRinging()) {
                           // 如果在来电响铃时按下电源键,则系统将关闭来电提示
                            telephonyService.silenceRinger();
                       }elseif ((mIncallPowerBehavior
                                & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0
                               && telephonyService.isOffhook()) {
                            // 如果处在通话中且电源键挂断选项已启用,则按下电源键会结束当前通话
                            hungUp = telephonyService.endCall();
                       }
                   }catch(RemoteException ex) {
                        Log.w(TAG,"ITelephony threw RemoteException", ex);
                   }
               }
               interceptPowerKeyDown(!isScreenOn || hungUp
                       || mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);
           }else{
               mPowerKeyTriggered = false;
               cancelPendingScreenshotChordAction();
               if(interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {
                   result = (result & ~ACTION_WAKE_UP) | ACTION_GO_TO_SLEEP;
               }
               mPendingPowerKeyUpCanceled = false;
           }
          break;
      }
....
....
....
}

上面的代码包含了对多种情形下对长按电源键时间的处理,例如静默来电响铃、屏幕截图以及关闭电源等。 系统将根据电源键被按住的时间长短以及相关按键的使用情况来决定如何恰当地处理当前的用户操作。 当电源键被按下且没有截屏操作触发时interceptPowerKeyDown 将被调用,这时其他的按键响应(其他按键响应指 interceptKeyBeforeQueueing 中其他cases)将不会被触发。

下面的代码展示了 interceptPowerKeyDown 函数内容, 函数将注册一个回调函数,在500毫秒超时事件(ViewConfiguration#getGlobalActionKeyTimeout())触发时启动 mPowerLongPress 线程

1
2
3
4
5
6
privatevoid interceptPowerKeyDown(booleanhandled) {
  mPowerKeyHandled = handled;
  if(!handled) {
       mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
  }
}

mPowerLongPress 线程的实现如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
privatefinal Runnable mPowerLongPress = newRunnable() {
        @Override
        publicvoid run() {
            // The context isn't read
            if(mLongPressOnPowerBehavior < 0) {
                mLongPressOnPowerBehavior = mContext.getResources().getInteger(
                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
            }
            intresolvedBehavior = mLongPressOnPowerBehavior;
            if(FactoryTest.isLongPressOnPowerOffEnabled()) {
                resolvedBehavior = LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM;
            }
 
            switch(resolvedBehavior) {
            caseLONG_PRESS_POWER_NOTHING:
                break;
            caseLONG_PRESS_POWER_GLOBAL_ACTIONS:
                mPowerKeyHandled = true;
                if(!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
                    performAuditoryFeedbackForAccessibilityIfNeed();
                }
                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
                showGlobalActionsDialog();
                break;
            caseLONG_PRESS_POWER_SHUT_OFF:
            caseLONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
                mPowerKeyHandled = true;
                performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
                mWindowManagerFuncs.shutdown(resolvedBehavior == LONG_PRESS_POWER_SHUT_OFF);
                break;
            }
        }
    };

 

第三步由上面代码的Switch分支可知,当程序进去Long_Press_Power_Global_Options控制将移交给 GlobalActions , 该模块则负责显示关机选项的对话框,这些选项在各Android发行版(各OEM厂商定制的Android系统, 不同的手机型号和不同版本的Android系统)中不尽相同,通常包括关闭电源、飞行模式和屏幕截图。也可能包括其他一些选项按键。GlobalActions 类实现了一个showdialog方法,该方法将根据当前系统支持的菜单内容来创建这个对话框。

1
2
3
4
5
6
7
8
9
10
11
voidshowGlobalActionsDialog() {
    if(mGlobalActions == null) {
        mGlobalActions = newGlobalActions(mContext, mWindowManagerFuncs);
    }
    finalboolean keyguardShowing = keyguardIsShowingTq();
    mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
    if(keyguardShowing) {
         // 由于激活关机对话框需要长按电源键两秒以上,所以当对话框显示之后,屏幕的唤醒状态将被锁定,以方便用户浏览对话框中内容
        mKeyguardMediator.userActivity();
    }
}

 

第四步若用户选择关闭电源,则对系统的控制将交回给 PhoneWindowManager, 然后由PhoneWindowManager 启动关闭流程

第五步: 整个关机过程起始于ShutdownThread模块中的shutdowninner方法。该方法首先创建一个确认对话框给用户, 用户可以选择确认关机或是取消关机操作。 如果用户选择确认,则系统将真正进入关机流程。

第六步如上所述,当用户点击确认按钮后beginShutdownSequence方法将被调用以启动关机顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
privatestatic void beginShutdownSequence(Context context) {
        synchronized(sIsStartedGuard) {
            if(sIsStarted) {
                Log.d(TAG,"Shutdown sequence already running, returning.");
                return;
            }
            sIsStarted = true;
        }
 
        // 显示正在关闭电源的对话框
        ProgressDialog pd = newProgressDialog(context);
        pd.setTitle(context.getText(com.android.internal.R.string.power_off));
        pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
 
        pd.show();
 
        sInstance.mContext = context;
        sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
 
        // 阻止CPU进入休眠状态
        sInstance.mCpuWakeLock = null;
        try{
            sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(
                    PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu");
            sInstance.mCpuWakeLock.setReferenceCounted(false);
            sInstance.mCpuWakeLock.acquire();
        }catch(SecurityException e) {
            Log.w(TAG,"No permission to acquire wake lock", e);
            sInstance.mCpuWakeLock = null;
        }
 
        // 电源关闭前一直保持屏幕唤醒状态,以便提升用户体验
        sInstance.mScreenWakeLock = null;
        if(sInstance.mPowerManager.isScreenOn()) {
            try{
                sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
                        PowerManager.FULL_WAKE_LOCK, TAG + "-screen");
                sInstance.mScreenWakeLock.setReferenceCounted(false);
                sInstance.mScreenWakeLock.acquire();
            }catch(SecurityException e) {
                Log.w(TAG,"No permission to acquire wake lock", e);
                sInstance.mScreenWakeLock = null;
            }
        }
 
        // 启动负责关机顺序的线程
        sInstance.mHandler = newHandler() {
        };
        sInstance.start();
    }

运行函数,启动实际的关机流程

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
publicvoid run() {
        BroadcastReceiver br = newBroadcastReceiver() {
            @Overridepublic void onReceive(Context context, Intent intent) {
                // We don't allow apps to cancel this, so ignore the result.
                actionDone();
            }
        };
 
        /*
         *  写入一个系统参数,以防Android系统中的System Server
         * (一个运行于Dalvik虚拟机与真实系统内核间的server,负责虚拟机与内核的通信)在真实硬件重启前完成重启。
         * 当上述情况发生时, 则在System Server完成启动后重试之前的重启操作。
         */
        {
            String reason = (mReboot ? "1" : "0") + (mRebootReason != null ? mRebootReason : "");
            SystemProperties.set(SHUTDOWN_ACTION_PROPERTY, reason);
        }
 
        /*
         * 写入一个系统参数以便重启后进入安全模式
         */
        if(mRebootSafeMode) {
            SystemProperties.set(REBOOT_SAFEMODE_PROPERTY,"1");
        }
 
        Log.i(TAG,"Sending shutdown broadcast...");
 
        // 关闭移动通信
        mActionDone = false;
        Intent intent = newIntent(Intent.ACTION_SHUTDOWN);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        mContext.sendOrderedBroadcastAsUser(intent,
                UserHandle.ALL,null, br, mHandler, 0,null,null);
 
        finallong endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
        synchronized(mActionDoneSync) {
            while(!mActionDone) {
                longdelay = endTime - SystemClock.elapsedRealtime();
                if(delay <= 0) {
                    Log.w(TAG,"Shutdown broadcast timed out");
                    break;
                }
                try{
                    mActionDoneSync.wait(delay);
                }catch(InterruptedException e) {
                }
            }
        }
 
        Log.i(TAG,"Shutting down activity manager...");
 
        finalIActivityManager am =
            ActivityManagerNative.asInterface(ServiceManager.checkService("activity"));
        if(am != null) {
            try{
                am.shutdown(MAX_BROADCAST_TIME);
            }catch(RemoteException e) {
            }
        }
 
        // 关闭移动通信
        shutdownRadios(MAX_RADIO_WAIT_TIME);
 
        // 安全移除外部存储卡
        IMountShutdownObserver observer = newIMountShutdownObserver.Stub() {
            publicvoid onShutDownComplete(intstatusCode) throwsRemoteException {
                Log.w(TAG,"Result code " + statusCode + " from MountService.shutdown");
                actionDone();
            }
        };
 
        Log.i(TAG,"Shutting down MountService");
 
        // 初始化变量,并设置关机超时时限
        mActionDone = false;
        finallong endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;
        synchronized(mActionDoneSync) {
            try{
                finalIMountService mount = IMountService.Stub.asInterface(
                        ServiceManager.checkService("mount"));
                if(mount != null) {
                    mount.shutdown(observer);
                }else{
                    Log.w(TAG,"MountService unavailable for shutdown");
                }
            }catch(Exception e) {
                Log.e(TAG,"Exception during MountService shutdown", e);
            }
            while(!mActionDone) {
                longdelay = endShutTime - SystemClock.elapsedRealtime();
                if(delay <= 0) {
                    Log.w(TAG,"Shutdown wait timed out");
                    break;
                }
                try{
                    mActionDoneSync.wait(delay);
                }catch(InterruptedException e) {
                }
            }
        }
 
        rebootOrShutdown(mReboot, mRebootReason);
    }

 

第七步: rebootOrShutdown方法被调用时,系统控制权首先转至底层函数 nativeShutdown(在com_android_server_power_PowerManagerService。cpp中定义) 并最终调用android_reboot函数(定义于android_reboot.c)来完成整个关机顺序

1
2
3
staticvoid nativeShutdown(JNIEnv *env, jclass clazz) {
    android_reboot(ANDROID_RB_POWEROFF,0,0);
}
0 0
原创粉丝点击