Android 实现耳机hook键长按

263 篇文章 2 订阅
129 篇文章 0 订阅
1、请在PhoneWindowManager.java文件中开始处修改
//Add blog.csdn.net/sergeycao
long preLastHeadsethookDownTime=0,preLastHeadsethookUpTime=0,
//Add blog.csdn.net/sergeycao
2、在interceptKeyBeforeQueueing方法中按照如下进行修改
int result;
//Add blog.csdn.net/sergeycao
if(keyCode == KeyEvent.KEYCODE_HEADSETHOOK ){ //KeyEvent.KEYCODE_VOLUME_DOWN
if(down){
//this is headsethook down 
thisHeadsethookDownTime = event.getEventTime();
}else{
thisHeadsethookUpTime = event.getEventTime();}
Log.d(TAG,"thisHeadsethookUpTime="+thisHeadsethookUpTime+",thisHeadsethookDownTime="+thisHeadsethookDownTime);
if((thisHeadsethookUpTime - thisHeadsethookDownTime) > 500){
keyCode == KeyEvent.KEYCODE_MEDIA_NEXT; 
}
//Add blog.csdn.net/sergeycao 
boolean isWakeKey = (policyFlags & WindowManagerPolicy.FLAG_WAKE) != 0
|| event.isWakeKey();
if (interactive || (isInjected && !isWakeKey)) {
// When the device is interactive or the key is injected pass the
// key to the application.
result = ACTION_PASS_TO_USER;
isWakeKey = false;
} else if (!interactive && shouldDispatchInputWhenNonInteractive()) {
// If we're currently dozing with the screen on and the keyguard showing, pass the key
// to the application but preserve its wake key status to make sure we still move
// from dozing to fully interactive if we would normally go from off to fully
// interactive.
result = ACTION_PASS_TO_USER;
} else {
// When the screen is off and the key is not injected, determine whether
// to wake the device but don't pass the key to the application.
result = 0;
if (isWakeKey && (!down || !isWakeKeyWhenScreenOff(keyCode))) {
isWakeKey = false;
}
}


// If the key would be handled globally, just return the result, don't worry about special
// key processing.
if (mGlobalKeyManager.shouldHandleGlobalKey(keyCode, event)) {
if (isWakeKey) {
mPowerManager.wakeUp(event.getEventTime());
}
return result;
}


boolean useHapticFeedback = down
&& (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
&& event.getRepeatCount() == 0;


Log.d(TAG, "interceptKeyTq keycode=" + keyCode
+ " interactive=" + interactive + " keyguardActive=" + keyguardActive
+ " policyFlags=" + Integer.toHexString(policyFlags)
+ " down =" + down + " canceled = " + canceled
+ " isWakeKey=" + isWakeKey
+ " mVolumeDownKeyTriggered =" + mVolumeDownKeyTriggered
+ " mVolumeUpKeyTriggered =" + mVolumeUpKeyTriggered
+ " result = " + result
+ " useHapticFeedback = " + useHapticFeedback
+ " isInjected = " + isInjected);
FM和Music对KeyEvent.KEYCODE_MEDIA_NEXT进行响应即可
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值