7

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

}
joepetrakovich
  • 1,344
  • 3
  • 24
  • 42

2 Answers2

6

The following should do the trick. BTW, where did you find the source code for the lock screen?

   public void handleMediaKeyEvent(KeyEvent keyEvent) {

    /*
     * Attempt to execute the following with reflection. 
     * 
     * [Code]
     * IAudioService audioService = IAudioService.Stub.asInterface(b);
     * audioService.dispatchMediaKeyEvent(keyEvent);
     */
    try {

        // Get binder from ServiceManager.checkService(String)
        IBinder iBinder  = (IBinder) Class.forName("android.os.ServiceManager")
        .getDeclaredMethod("checkService",String.class)
        .invoke(null, Context.AUDIO_SERVICE);

        // get audioService from IAudioService.Stub.asInterface(IBinder)
        Object audioService  = Class.forName("android.media.IAudioService$Stub")
                .getDeclaredMethod("asInterface",IBinder.class)
                .invoke(null,iBinder);

        // Dispatch keyEvent using IAudioService.dispatchMediaKeyEvent(KeyEvent)
        Class.forName("android.media.IAudioService")
        .getDeclaredMethod("dispatchMediaKeyEvent",KeyEvent.class)
        .invoke(audioService, keyEvent);            

    }  catch (Exception e1) {
        e1.printStackTrace();
    }
}
user1937272
  • 61
  • 1
  • 2
  • There is an answer related to this one which does not reflection. it simply uses AudioManager. http://stackoverflow.com/a/40493319/1713704 – MojAmiri Nov 09 '16 at 14:45
2

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.

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
  • I haven't tried with an accessory because I don't have one. Wish I did. Is there a way I could find out the component name of the current music playing app? I don't know if it's possible to make an intelligent guess since sending an ordered broadcast depends on the app's priority. I assume eventually the other apps like Pandora will update to the new API... – joepetrakovich Oct 06 '12 at 19:57
  • You can always see which applications are running using [ActivityManager.getRunningTasks(int)](http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int)) but as a user I prefer settings (maybe backed up with some "AI" to provide suggestions and/or defaults. There is also the alternative to look in logs, but I wouldn't propose that since it is very unreliable. For testing without an accessory you can look [here](http://code.google.com/p/media-button-router/wiki/TestingWithoutBluetooth) I haven't tested these my self though. – Tobias Ritzau Oct 09 '12 at 19:04
  • So I was able to try with a real accessory. It works properly using a samsung bluetooth headset. The current foreground app, Pandora, responds to the play pause and next buttons on the headset. Do headsets have some kind of special access? How can I construct my intent to act as though it came from a headset? – joepetrakovich Oct 10 '12 at 20:55
  • Using the media buttons app you pointed me to shows the same behavior as my app. Google Play just pops open and uses the intent rather than Pandora or Spotify. – joepetrakovich Oct 10 '12 at 21:05
  • Ok, what happens is that pandora registers itself in the audio service (private api), and the PhoneWindowManager (hidden) calls the audio service to invoke the pending intent that was created during registration. All of this is hidden. But it would be interesting to see what happens if you could inject the event somehow. Injecting it to an activity would probably not be of any good and that is what I have found so far Activity.dispatchKeyEvent(). I'll keep looking. – Tobias Ritzau Oct 10 '12 at 21:33