当前位置:操作系统 > 安卓/Android >>

Android关机流程源码分析

Android系统开机时,在启动SurfaceFlinger服务过程中通过Android属性系统方式来启动bootanim进程,实现开机动画显示过程;当系统关机时,又是如何启动关机动画的呢?Android系统的整个关机流程又是怎样的呢?本文就针对这两个问题透过源码来给出具体的分析。我们知道,当长按电源键,系统会弹出关机提示对话框
 
当点击选择关机时,系统就会完成整个关机流程。接下来就通过源码来介绍Android关机流程的完整实现过程。当长按电源键时,按键消息被分发到PhoneWindowManager的interceptKeyBeforeQueueing函数中处理:
 
[java]  
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {  
    ...  
    switch (keyCode) {  
        ...  
        case KeyEvent.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();  
                boolean hungUp = false;  
                if (telephonyService != null) {  
                    try {  
                        if (telephonyService.isRinging()) {  
                            //当来电时按下电源键,启动静音  
                            telephonyService.silenceRinger();  
                        } else if ((mIncallPowerBehavior  
                                & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0  
                                && telephonyService.isOffhook()) {  
                            // Otherwise, if "Power button ends call" is enabled,  
                            // the Power button will hang up any current active call.  
                            hungUp = telephonyService.endCall();  
                        }  
                    } catch (RemoteException ex) {  
                        Log.w(TAG, "ITelephony threw RemoteException", ex);  
                    }  
                }  
                interceptPowerKeyDown(!isScreenOn || hungUp  
                        || mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);  
            } else {  
                ...  
            }  
            break;  
        }  
        ...  
    }  
    return result;  
}  
电源键和音量键的组合可以实现特定功能,比如按下电源键和音量向下键,可实现抓屏,interceptKeyBeforeQueueing函数首先根据条件处理电源键按下的特定任务,然后调用interceptPowerKeyDown做进一步处理
[java]  
private void interceptPowerKeyDown(boolean handled) {  
    mPowerKeyHandled = handled;  
    if (!handled) {  
        //隔500ms处理电源按键事件  
        mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());  
    }  
}  
这里的mHandler是在初始化PhoneWindowManager对象时创建的
[java]  
public void init(Context context, IWindowManager windowManager,WindowManagerFuncs windowManagerFuncs,  
        LocalPowerManager powerManager) {  
    ...  
    mHandler = new PolicyHandler();  
    ...  
}  
将一个Runnable对象mPowerLongPress发送到PolicyHandler中进行处理
[java]  
private final Runnable mPowerLongPress = new Runnable() {  
    public void run() {  
        // The context isn't read  
        if (mLongPressOnPowerBehavior < 0) {  
            mLongPressOnPowerBehavior = mContext.getResources().getInteger(  
                    com.android.internal.R.integer.config_longPressOnPowerBehavior);  
        }  
        switch (mLongPressOnPowerBehavior) {  
        case LONG_PRESS_POWER_NOTHING:  
            break;  
        case LONG_PRESS_POWER_GLOBAL_ACTIONS:  
            mPowerKeyHandled = true;  
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
            showGlobalActionsDialog();  
            break;  
        case LONG_PRESS_POWER_SHUT_OFF:  
            mPowerKeyHandled = true;  
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
            sendCloseSystemWindo
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,