Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:36:29+00:00 2026-06-12T11:36:29+00:00

When sending a sendOrderedBroadcast with an ACTION_MEDIA_BUTTON intent (I’m imitating that the user is

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:36:30+00:00Added an answer on June 12, 2026 at 11:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sending an ordered broadcast to specific media players, so that I can
am sending one user object from java to flex using remote object,now i want
when sending mails from intent we are calling the intent using such as these
After sending the user to safari to authorize, i get sent to mobile.twitter.com as
Sending XML from C# Server and receiving it it in Android Java client This
Sending the same pointer into two different shared_ptr 's is bad, it causes double
sending text to label and number to int from tabelview to viewcontroller but it
sending mail along with embedded image using asp.net I have already used following but
Sending Email in Android using JavaMail API without using the default/built-in app I'm trying
Im sending (with ASIFormDataRequest) a POST request, with 4 parameters. The server launches a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.