10

Possible Duplicate:
Listen to volume buttons in background service?

I want to listen to volume key changes in my application even with the locked screen and display powered off. The main problem that the service runs all time (with the help of wake locks) but I can read only the volume changes with my approach.

If I use a service that runs all time (even with the locked screen and turned off display) I have a problem. It has broadcast receiver for volume changes with the help of this post.

public BroadcastReceiver MediaButton_Receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        int action = event.getAction();
        if (action == KeyEvent.KEYCODE_VOLUME_DOWN) {
            Log.d(TAG,"VOLUME_DOWN Pressed");   
        }
    };
};

event is always null in the service. It only calls when the volume is changed. Also BroadcastReceiver doesn't work when the screen is off.

Another way to do it is to use an Activity with that overrides dispatchKeyEvent() but my Activity runs only a little bit, I tried wake lock, it didn't help. And I'm still not sure that it will work with the locked screen. I have searched all over the Internet but I cannot find the answer.

Can I create an Activity that will catch pressing the volume buttons even with the locked and turned off screen? Or can I somehow do it with the service?

Community
  • 1
  • 1
b-boy sh1ft
  • 179
  • 8
  • 1
    I'm looking for this since long and couldn't find anything either! Cannot be done from an activity if the screen is off/locked. Probably the solution is a broadcast receiver, but not sure if you can catch volume buttons press from there... – Sdra Oct 02 '12 at 10:39
  • BroadcastReceiver working only with screen ON and it can tell me if the volume WAS changed:( For now I am trying to use [InputMethodService](http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html) for hard keys, but onKeyUp() method didn't work at all – b-boy sh1ft Oct 02 '12 at 12:17
  • 1
    look here: http://stackoverflow.com/questions/10154118/listen-to-volume-buttons-in-background-service – fecub Oct 05 '12 at 09:07
  • did u manage to get this solved?? I tried a broadcast receiver too, with intent actions bt then doesnt work :(.. ny way out ?? – Rat-a-tat-a-tat Ratatouille Mar 19 '14 at 05:05
  • @Rat-a-tat-a-tatRatatouille : Have you found the soln? I have a similar prob. – Basher51 Sep 11 '14 at 02:33
  • @Basher51 The final result I've got - you cannot do this – b-boy sh1ft Sep 11 '14 at 14:49
  • @Basher51 - lol no i could not get the solution to this. It depends on the hardware make. – Rat-a-tat-a-tat Ratatouille Sep 12 '14 at 05:07

0 Answers0