Listen to Volume Buttons in Background Service

Listen to volume buttons in background service?

Judging by the couple of other questions about this topic, no.

Other question 1,
Other question 2

Services simply do not receive KeyEvent callbacks.

Listen to volume buttons in background service?

Judging by the couple of other questions about this topic, no.

Other question 1,
Other question 2

Services simply do not receive KeyEvent callbacks.

Android How to listen for Volume Button events?

I don't know if you can get long press events for the hardware keys.

I've used this code to listen for the volume button before.

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

If that doesn't work for you let us know what device you are testing on.

Kotlin

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// Do something
}
return true
}

how to create a service, that would be listening to the volume button?

What do you think, is that possible?

No. You cannot listen for volume button presses in a service.



Related Topics



Leave a reply



Submit