前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
如果你需要做双击 home key 的检测
你可以参考如下 sample code 自行实现
请修改 phoneWindowManager.java 的 interceptKeyBeforeQueueing 方法
找到参考位置后, 加入注释所述代码:
找到参考位置后, 加入注释所述代码:
//以下是原有代码, 即参考行
if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
&& event.getRepeatCount() == 0) {
performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
}
//参考行到此结束
int result = 0; //原为 int result, 请加入初始值.// mtk sample code for double home click detection BEGIN.
//请在类中补充 boolean homeDownDoubleClick = false; 的定义
//请在类中补充 long lastHomeDownTime=0; 的定义
//请在类中补充 long lastHomeUpTime=0; 的定义
//检测原理: 检测上一次按下的 home key 与本次按下的 home key 时间间隔是否 < 500ms
//if yes, 则认为是双击 home key
if(keyCode == KeyEvent.KEYCODE_HOME){
if(down){
// this is home down
if(((event.getEventTime() - lastHomeDownTime) < 500)){
homeDownDoubleClick = true;
}else{
homeDownDoubleClick = false;
}
lastHomeDownTime = event.getEventTime();
}else{
// then home up comes
Log.d(TAG,"homeDownDoubleClick="+homeDownDoubleClick+",lastHomeDownTime="+lastHomeDownTime+",lastHomeUpTime="+lastHomeUpTime+",this home up="+event.getEventTime());
if(homeDownDoubleClick && ((event.getEventTime() - lastHomeUpTime) < 500)){
Log.d(TAG,"double click on home detected");
try {
IStatusBarService statusbar = getStatusBarService();
if (statusbar != null) {
statusbar.preloadRecentApps();
statusbar.toggleRecentApps();
}
} catch (RemoteException e) {
Slog.e(TAG, "RemoteException when preloading recent apps", e);
mStatusBarService = null;
}
result |= ACTION_WAKE_UP;
return result;
}
lastHomeUpTime = event.getEventTime();
}
}
// mtk sample code for double home click detection END.