捕捉Power键的长按与短按处理

1. PhoneWindowManager.java中监听KeyEvent.KEYCODE_POWER事件(代码:mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(KeyEvent.KEYCODE_POWER)

2.在interceptKeyBeforeQueueing中会有关KeyEvent.KEYCODE_POWER事件的处理

            case KeyEvent.KEYCODE_POWER: {
                result &= ~ACTION_PASS_TO_USER;
                if (down) {
                    ITelephony telephonyService = getTelephonyService();
                    boolean hungUp = false;
                    if (telephonyService != null) {
                        try {
                            if (telephonyService.isRinging()) {
                                // Pressing Power while there's a ringing incoming
                                // call should silence the ringer.
                                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);
					// 在测试长按时,一般来说isScreenOn为true,hungUp即电话挂断状态一般为false
                } else {
                    if (interceptPowerKeyUp(canceled)) {
                        result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
                    }
                }
                break;
            }
3.interceptPowerKeyDown函数代码如下:

    private void interceptPowerKeyDown(boolean handled) {
        mPowerKeyHandled = handled;
        if (!handled) {
            mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
        }
    }

据分析传进来的handled为false,这些它会发出一个Delay消息,因为长按会有500毫秒的等待时间。

    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();
            }
        }
    };

从代码sendCloseSystemWindows可以看出,为什么长按会拉起一个对话框,是这个函数调起来的        
有Down事件,肯定有Up事件,处理Up事件是由interceptPowerKeyUp完成的

private boolean interceptPowerKeyUp(boolean canceled) {
        if (!mPowerKeyHandled) {
		    // 如果不是长按的话,就会remove掉长按处理,因为只响应短按了
            mHandler.removeCallbacks(mPowerLongPress);
            return !canceled;
        } else {
            mPowerKeyHandled = true;
            return false;
        }
    }



你可能感兴趣的:(捕捉Power键的长按与短按处理)