您的位置:首页 > 移动开发 > Android开发

Android 实现耳机hook键长按

2016-04-05 15:04 483 查看
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进行响应即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: