Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The BroadcastReceiver for the ACTION_MEDIA_BUTTON intent fires TWICE per MediaButton click

I have a simple but probably badly structured Android application. It consists of two java classes: MainActivity extending Activity, and the RemoteControlReceiver that extends BroadcastReceiver.

I have followed the instruction in the following two links to set up the Mediabutton receiver: http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html http://developer.android.com/training/managing-audio/volume-playback.html

The issue is that whenever I press a media button (play/pause, next, previous) on my bluetooth remote, the broadcastReceiver's onReceive() method runs twice. Or more specifically the whole RemoteControlReceiver gets initialized into object, the object's onReceive() method runs, the object gets scrapped, and repeat.

I tested for this by placing a static int mult = 0; in the MainActivity. I incremented the mult by 1 at every run of onReceive. And it increments twice per button click.

I am not sure what causes it to run twice. Is the hardware sending double signal per click, or is the OS sending multiple media button intent per signal, or is my broadcastreceiver running twice per intent?

My MediaButtonReceiver code:

public class RemoteControlReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())){
            KeyEvent Xevent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            int keyType = Xevent.getKeyCode();



            Intent i = new Intent();
            i.setAction("com.MainActivity.Shakey.MEDIA_BUTTON");
            i.putExtra("keyType", keyType);
            context.sendBroadcast(i);
            Toast.makeText(context, String.valueOf(MainActivity.mult), Toast.LENGTH_SHORT).show();
            MainActivity.mult++;
            abortBroadcast();

        }

    }
}

This receiver's filter is registered in the Manifest as follows:

<application>
 ...
   <receiver android:name=".RemoteControlReceiver">
       <intent-filter>
           <action android:name="android.intent.action.MEDIA_BUTTON"/>
       </intent-filter>
   </receiver>
 ...
</application>

The Broadcastreceiver is dynamically registered to the AudioManager object in the MainActivity's onResume(). And it is unregistered in onPause(). As the links said this is a sure way to get 1st priority on the media_button intents. I know I can ignore every even call of the broadcastReceiver by the use of a static variable. But I would like to know the cause of this issue.

PS The play/pause/previous/next buttons work fine for the default android music player.

like image 407
user2275463 Avatar asked Apr 12 '13 22:04

user2275463


1 Answers

In my opinion the problem is, that OnReceive reacts on pressing and releasing bluetooth button. Thats why your code runs 2 times. Try to do sth like this:

    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
    KeyEvent Xevent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);    
    if ((KeyEvent.KEYCODE_MEDIA_PLAY == Xevent.getKeyCode()) && (Xevent.getAction() == KeyEvent.ACTION_DOWN)) {  //MainActivity.mult++; ...

Or u can use ACTION_UP in spite of what u want to do. Im not sure, but I hope this will help u.

like image 77
user2279471 Avatar answered Nov 16 '22 09:11

user2279471