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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With