Android - Getting volume button long clicks

一笑奈何 提交于 2019-11-28 11:48:38

问题


Can someone please show me a code example about how to get a long click (2 sec for example) on the volume up hardware key?

Thanks :)

EDIT

The class that i want to capture the long click with is a Service. How can i do that?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10685502/android-getting-volume-button-long-clicks

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