Android 实现耳机hook键长按

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进行响应即可

你可能感兴趣的:(android,ARM,mobile)