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

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


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

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

[java]  view plain copy
  1. case KeyEvent.KEYCODE_POWER: {  
  2.     result &= ~ACTION_PASS_TO_USER;  
  3.     if (down) {  
  4.         ITelephony telephonyService = getTelephonyService();  
  5.         boolean hungUp = false;  
  6.         if (telephonyService != null) {  
  7.             try {  
  8.                 if (telephonyService.isRinging()) {  
  9.                     // Pressing Power while there's a ringing incoming  
  10.                     // call should silence the ringer.  
  11.                     telephonyService.silenceRinger();  
  12.                 } else if ((mIncallPowerBehavior  
  13.                         & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0  
  14.                         && telephonyService.isOffhook()) {  
  15.                     // Otherwise, if "Power button ends call" is enabled,  
  16.                     // the Power button will hang up any current active call.  
  17.                     hungUp = telephonyService.endCall();  
  18.                 }  
  19.             } catch (RemoteException ex) {  
  20.                 Log.w(TAG, "ITelephony threw RemoteException", ex);  
  21.             }  
  22.         }  
  23.   
  24.         interceptPowerKeyDown(!isScreenOn || hungUp);  
  25. 按时,一般来说isScreenOn为true,hungUp即电话挂断状态一般为false  
  26.     } else {  
  27.         if (interceptPowerKeyUp(canceled)) {  
  28.             result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;  
  29.         }  
  30.     }  
  31.     break;  
  32. }  
3.interceptPowerKeyDown函数代码如下:

[java]  view plain copy
  1. private void interceptPowerKeyDown(boolean handled) {  
  2.     mPowerKeyHandled = handled;  
  3.     if (!handled) {  
  4.         mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());  
  5.     }  
  6. }  

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

[java]  view plain copy
  1. private final Runnable mPowerLongPress = new Runnable() {  
  2.     public void run() {  
  3.         if (!mPowerKeyHandled) {  
  4.             mPowerKeyHandled = true;  
  5.             performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
  6.             sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
  7.             showGlobalActionsDialog();  
  8.         }  
  9.     }  
  10. };  

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

[java]  view plain copy
  1. private boolean interceptPowerKeyUp(boolean canceled) {  
  2.         if (!mPowerKeyHandled) {  
  3.             // 如果不是长按的话,就会remove掉长按处理,因为只响应短按了  
  4.             mHandler.removeCallbacks(mPowerLongPress);  
  5.             return !canceled;  
  6.         } else {  
  7.             mPowerKeyHandled = true;  
  8.             return false;  
  9.         }  
  10.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值