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?