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

I need to create a service that would be running in the background and detect hardware click. edit: found this snippet that works great for an activity, now will have to try it with a 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); } }

SUBMIT YOUR ANSWER

  |  
 
 
 

Preview :