且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

双击检测耳机按钮并长按(单击)Android

更新时间:2023-11-18 12:43:40

对于双击,您可以通过重写Activity onKeyDown方法并测量每次单击之间经过的时间来检测到它:

For the double click you can detect it by overriding Activity onKeyDown method and measuring time elapsed between each click:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_HEADSETHOOK){
        //check if it's the first or the second click and measure time between them
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

还有一个Activity onKeyLongPress方法,但是它在带有耳机按钮的设备上似乎不起作用,因为当我长按耳机按钮时,它会启动Google即时,而我在活动中无法检测到

There's also an Activity onKeyLongPress method but it doesn't seem to work on my device with headset button as when I long press headset button it launches Google Now and I can't detect it inside my activity