Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerMediaButtonEventReceiver not receiving events

Tags:

android

volume

I am trying to receive the physical button events such as volume button clicks on the side of the phone. I need to do that in a Service because I want to receive if the user clicks on the Volume up/down button at any time. I read a lot in the net, but I cannot make it work and don't know what could 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 service OnStart():

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

An the broascastreceiver:

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

} 
like image 481
Ton Avatar asked Jun 04 '13 19:06

Ton


2 Answers

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

like image 162
matrixsmurfen Avatar answered Oct 19 '22 07:10

matrixsmurfen


I know that this is an old Question, I came across this same issue and found that we must register the registerMediaButtonEventReceiver(..) only after holding the AudioManager.AUDIOFOCUS_GAIN otherwise it is not working.

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>
like image 21
mifthi Avatar answered Oct 19 '22 07:10

mifthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!