joepetrakovich joepetrakovich - 28 days ago 17
Android Question

Is Google Play Music hogging all ACTION_MEDIA_BUTTON intents?

When sending a sendOrderedBroadcast with an ACTION_MEDIA_BUTTON intent (I'm imitating that the user is clicking the play button on a bluetooth headset), Google Play Music opens and plays the last album played instead of the foreground music playing app.

If I change it to sendBroadcast, both Google Play Music AND the current music playing app (Pandora in my case), will enact the play button.

This only occurs in Android 4.0 and above. Is Play Music hogging this intent (a bug)? Do you suspect that Pandora is not registering itself as the current media button handler following this advice:
http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html

Is there a way I can direct this intent to the current music playing app only?

Here is my code:

public void broadcastMediaIntent(MediaIntent intentType){

long eventtime = SystemClock.uptimeMillis();

Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);

KeyEvent downEvent = null;
KeyEvent upEvent = null;

switch(intentType){
case NEXT:

downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);

upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);

break;
case PLAY_PAUSE:

downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);

upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);

break;
case PREVIOUS:

downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);

upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
break;
case FAST_FORWARD:

downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0);

upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0);
break;
case REWIND:

downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_REWIND, 0);

upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_REWIND, 0);
break;

default:
break;
}

downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);

upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);

}

Answer

There is an API that you have to use to be the preferred receiver of those intents, but that can of course be handled by the sender of the system keys. Look at this post, and the docs. But that is up to pandora and google music in this case so that is probably not up to you. You could of course also send your broadcasts to a particular package (by specifying component name in the intent), but then you decide which app gets it. I did a quick search for a hidden API in the AudioManager but that doesn't look to promising.

Have you tried with an accessory? If that works then I would look into how that intent is sent. If it doesn't I would take a look at which applications are installed and either make an "intelligent" guess or ask the user for which app to send the intent to. Maybe the last one is the best way to go in either case since it uses public APIs and will not annoy the user by guessing wrong :)

Edit: This could work, but it may also be guarded by permissions and certificates. In the lockscreen this is how media keys are handled:

void handleMediaKeyEvent(KeyEvent keyEvent) {
    IAudioService audioService = IAudioService.Stub.asInterface(
            ServiceManager.checkService(Context.AUDIO_SERVICE));
    if (audioService != null) {
        try {
            audioService.dispatchMediaKeyEvent(keyEvent);
        } catch (RemoteException e) {
            Log.e("KeyguardViewBase", "dispatchMediaKeyEvent threw exception " + e);
        }
    } else {
        Slog.w("KeyguardViewBase", "Unable to find IAudioService for media key event");
    }
}

However, the API is hidden so you would have to work around that. That is probably the best I can help you. The alternative is to inject the events. One way to do that is to become an input method, but that is not likely to be a way forward. There are several ways to inject events to your own activity, but I don't know of any that injects to the system. Maybe you can take a look at how the instrumentation tests are doing it.