How to create a service that will listen to the volume button?

I need to create a service that will run in the background, display an icon on the notification panel, listen to the volume buttons and prevent their volume changes, so that the annoying sound signal will not be heard.

Do you think this is possible? And if so, where do I start?

Thanks!

edit:

found this fragment that works great for activity, now you have to try it using the service.

@Override public boolean dispatchKeyEvent(KeyEvent event) { int action = event.getAction(); int keyCode = event.getKeyCode(); switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: if (action == KeyEvent.ACTION_UP) { Toast.makeText(this, "UP", 1500).show(); } return true; case KeyEvent.KEYCODE_VOLUME_DOWN: if (action == KeyEvent.ACTION_DOWN) { Toast.makeText(this, "DOWN", 1500).show(); } return true; default: return super.dispatchKeyEvent(event); } } 

edit2: found a good tutorial here http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/

created a service, but the code above doesn't seem to fit into it ... hmm ...

+4
source share
1 answer

Do you think this is possible?

No. You cannot listen to the volume buttons in the service.

+3
source

Source: https://habr.com/ru/post/1393595/


All Articles