RegisterMediaButtonEventReceiver does not accept events

I am trying to get physical button events, such as clicks of the volume button on the side of the phone. I need to do this in the service because I want to receive if the user presses the volume up / down button at any time. I read a lot on the net, but I can't get it to work and I don't know what might be wrong. This is my code:

In the manifest:

<receiver android:name=".clsMediaButtonReceiver" > <intent-filter android:priority="1000" > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> 

In the OnStart () service:

  AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE); manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), clsMediaButtonReceiver.class.getName())); 

Interacting:

 public class clsMediaButtonReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //This is never shown Toast toast1 = Toast.makeText(Context, intent.getAction(), Toast.LENGTH_SHORT); toast1.show(); } } 
+7
source share
3 answers

I added <action android:name="android.media.VOLUME_CHANGED_ACTION" /> to my intent filter, and now I get volume key events. I have yet to figure out how to properly distribute them in my application, though ... I hope this helps!

+9
source

I know this is an old question, I ran into this problem and found that we should register registerMediaButtonEventReceiver (..) only after we hold AudioManager.AUDIOFOCUS_GAIN, otherwise it does not work.

I tried with the same Manifest and BroadcastReciever written by @Ton

 private class ClassOnAudioFocusChangeListener implements AudioManager.OnAudioFocusChangeListener { @Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){ Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS_TRANSIENT"); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_GAIN"); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonEventComponenName); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS"); } } }; 

Here is the same manifest.

  <receiver android:name="MainActivity$MediaButtonEventReceiver"> <intent-filter android:priority="1000" > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> 
+4
source

I have the same problem as you, and I would like to know if you allowed it. The difference with my code is that I am a broadcastReceiver class inside another class. Something like that:

 public class MyClass extends Activity { /// Code public class MediaButtonReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { /// Code } } } 

And the manifest:

 <receiver android:name="net.mypackage.MyClass.MediaButtonReceiver"> <intent-filter android:priority="1000" > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> 

thanks for the help

0
source

All Articles