Roger Travis
Roger Travis

Reputation: 8538

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, displaying an icon in the notification bar, listen to the volume buttons being presses and prevent them from changing the volume, so that annoying beep won't be heard.

What do you think, is that possible? And if yes, where should I start?

Thanks!

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);
        }
    }

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

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

Upvotes: 3

Views: 1924

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006564

What do you think, is that possible?

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

Upvotes: 3

Related Questions