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 7490435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:36:18+00:00 2026-05-29T15:36:18+00:00

I studied the ACTION_MEDIA_BUTTON intent and Im trying to use it and intercept the

  • 0

I studied the ACTION_MEDIA_BUTTON intent and Im trying to use it and intercept the button presses and present them on screen using a toast. I registered the receiver to intercept two intents:

  1. ACTION_HEADSET_PLUG – plugging the headset

  2. ACTION_MEDIA_BUTTON – receiving the button presses

This is done in my main activity:

        IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
        mediaFilter.setPriority(10000);
        registerReceiver(_receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
        registerReceiver(_receiver, mediaFilter);

This is the part of the receiver that handles the button presses:

    if (action.equals(Intent.ACTION_HEADSET_PLUG))
    {
        Toast.makeText(context, "earphones activity",Toast.LENGTH_SHORT).show();
        if (intent.getExtras().getInt("state")==1)//if plugged
            Toast.makeText(context, "earphones plugged",Toast.LENGTH_LONG).show();
        else Toast.makeText(context, "earphones un-plugged",Toast.LENGTH_LONG).show();
    }
    else 
    if (action.equals(Intent.ACTION_MEDIA_BUTTON))
    {
        Toast.makeText(context, "button pressed",Toast.LENGTH_LONG).show();
        key=intent.getExtras().getString("EXTRA_KEY_EVENT");
        Toast.makeText(context, key,Toast.LENGTH_LONG).show();
    }

Now the part that handles the headset plug-in and removal works fine, but the part that intercept the button press isn’t.

Is there any reason the code that handles the ACTION_MEDIA_BUTTON doesn’t work?

Is there a special permission I need to intercept such an intent?

I’m using a Samsung Galaxy S2 to test the code.

I’ve looked at all the similar posts and tried everything. Unfortunately nothing seems to work.

  • 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-05-29T15:36:20+00:00Added an answer on May 29, 2026 at 3:36 pm

    I recently developed an application which responded to the media button. I tested it in the Samsung Galaxy S II, and it worked.

    First, in your AndroidManifest.xml, inside the <application> area, place the following:

    <!-- Broadcast Receivers -->
    <receiver android:name="net.work.box.controller.receivers.RemoteControlReceiver" >
        <intent-filter android:priority="1000000000000000" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
    

    Then, create a BroadcastReceiver in a different file:

    public class RemoteControlReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) {
                KeyEvent event = (KeyEvent) intent .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    
                if (event == null) {
                    return;
                }
    
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    context.sendBroadcast(new Intent(Intents.ACTION_PLAYER_PAUSE));
                }
            }
        }
    
    }
    

    This is probably not the best solution out there (specially the hard coded android:priority above). However, it tried a couple of other techniques and none of them seemed to work. So I have to resort to this one… I hope I helped.

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

Sidebar

Related Questions

i have studied design patterns and want to use them to code an open
I've studied some CQRS sample implementations (Java / .Net) which use event sourcing as
I studied the Twitter API Documentation today. Only find that we could use Twitter
Having briefly studied databases in college, I haven't worked with them since and have
I studied many forums, some of them saying that I am not allowed and
Some time ago i was pretty interested in GAs and i studied about them
I have studied examples of using mathlink from Visual Studio. The question is, I
I've studied C programming in college some years ago and have developed some medium
We have studied the Variable Elimination recently and the teacher emphasizes that it is
I have studied generators feature and i think i got it but i would

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.