Android - Getting volume button long clicks

五迷三道 提交于 2019-11-29 18:06:01
MAV

If you just need to capture long clicks, this answer might be helpful:

https://stackoverflow.com/a/5269673/1401257

EDIT:

I have never tried to have a key listener inside a service, but with a little help from Google I found this: Volume change listener?

It seems that normal key events can only be handled from Activities. I do not have time to try this out myself, but for capturing long clicks it might be possible to combine the answer from the link and Lukes answer. From what I understand about BroadcastReceivers, you would want to create a receiver, that notify the Service whenever someone click the volume buttons.

Optionally you could do something like this:

if(clickedDown) {
if(beginningTime + 2000 < System.currentTimeMillis()) {
// Ok, the button has been clicked down for 2 seconds
}
}
else {
beginningTime = System.currentTimeMillis();
}

Applying something like this, you'll be able to define the amount of time to wait.

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