尝试捕捉 onKeyLongPress() 的音量不起作用
我试图让我的应用程序使用以下代码对音量调低时的长按键做出反应:
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("myApp", "LONG PRESS");
}
return super.onKeyLongPress(keyCode, event);
}
但是,它只会发送一堆用于调低音量的 onKeyPress()
事件,并且 onKeyLongPress() 永远不会被调用。我的目的是让音量调低和调高“短按”单独进行,并让我的应用程序对音量长按做出不同的反应。
有人能指出我缺少什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了。您需要在 onKeyPress 处理程序中检测相同的按键事件并开始跟踪它,以便长按可以起作用。这是您需要的代码:
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: