Trying to catch the Volume onKeyLongPress() not working

十年热恋 提交于 2019-12-17 21:04:24

问题


I'm trying to get my app to react to a long key press on volume down with the following code:

public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        Log.w("myApp", "LONG PRESS");
    }
    return super.onKeyLongPress(keyCode, event);
}

However, it only spams a bunch of onKeyPress() events for volume down, and onKeyLongPress() never gets called. My intention is to leave the volume down and up "short" presses alone, and have my app react differently to the volume long press.
Can anybody point out what I'm missing?


回答1:


You are almost there. You need to detect the same key event in the onKeyPress handler and start tracking it so that the long press can work. Here's the code you need:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


来源:https://stackoverflow.com/questions/7493531/trying-to-catch-the-volume-onkeylongpress-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!