BroadcastReceiver for ACTION_MEDIA_BUTTON purposes fires TWICE on one click MediaButton

I have a simple but probably poorly structured Android app. It consists of two java classes: MainActivity, which extend the Activity, and RemoteControlReceiver, which extends the BroadcastReceiver.

I followed the instructions in the following two links to configure 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 problem is that whenever I press the multimedia button (play / pause, next, previous) on my remote firewall, the broadcastReceiver onReceive () method is executed twice. Or, more specifically, the entire RemoteControlReceiver is initialized to an object, the onReceive () object is launched, the object is interrupted and repeated.

I tested this by setting a static int mult = 0; in MainActivity. I increased the number by 1 every time I run onReceive. And it increases by two button presses.

I'm not sure what makes it work twice. Is the hardware a two-click signal or does the OS send several multimedia button signals per signal or does my translator work twice with each intention?

My MediaButtonReceiver code is:

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 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>

Broadcastreceiver AudioManager MainActivity onResume(). onPause(). , 1- -. , . .

PS play/pause/previous/next .

+5
1

-, , OnReceive Bluetooth. 2 . :

    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++; ...

ACTION_UP, , . , , .

+12

All Articles