2

I've found a lot of pages about this in the web, but none of them helped me. I've been for hours stucked in this problem. That's why i decided to make my own question.

What I want to do is an application that receives an intent of the type ACTION_MEDIA_BUTTON and the method onReceive() of the Broadcastreceiver does something.

My Activity is like this:

public class MusicControlActivity extends Activity {

private MediaButtonIntentReceiver receiver = new MediaButtonIntentReceiver();
    @Override
    public void onCreate(Bundle p_SavedInstanceState) {
        super.onCreate(p_SavedInstanceState);
        setContentView(R.layout.main);


        IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
        filter.setPriority(1000);
        registerReceiver(receiver,filter);
}

@Override
public void onDestroy()
{
    unregisterReceiver(receiver);
}

And my broadcastreceiver as follow:

public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() 
{
    super();
}

@Override
public void onReceive(Context context, Intent intent) 
{
    String v_IntentAction = intent.getAction();
    if (!Intent.ACTION_MEDIA_BUTTON.equals(v_IntentAction)) {
        return;
    }
    KeyEvent v_Event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (v_Event == null) {
        return;
    }
    int v_Action = v_Event.getAction();
    if (v_Action == KeyEvent.ACTION_DOWN) {
    // do something
        Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
    }
    abortBroadcast();
}
}

The problem is that it doesn't work, no matter what I do. I've tried registering it dynamically with registerReceiver as in the code above. Also I've tried statically in the AndroidManifest.xml like this:

<receiver android:name=".MediaButtonIntentReceiver" android:enabled="true">
    <intent-filter android:priority="10000000">
        <action android:name="android.intent.action.ACTION_MEDIA_BUTTON" />
    </intent-filter>
</receiver>

And as you can see I've set the priority to a high level and even though it doesn't work. I've been on this for the whole day and I don't know what to do. The method onReceive from the broadcastreceiver isn't called anytime.

Does anyone know what should I do?

Guilherme Gusman
  • 556
  • 2
  • 7
  • 17

2 Answers2

2

Use android.intent.action.MEDIA_BUTTON Instead in your manifest. Check : http://developer.android.com/training/managing-audio/volume-playback.html

Mehul Shah
  • 391
  • 3
  • 7
0

First, you should stop using Toast as your diagnostic test. Use the Log class to log to LogCat, or breakpoints, or something.

Next, you should get rid of your MediaButtonIntentReceiver constructor, as it is not needed.

Then, I would dump the if (!Intent.ACTION_MEDIA_BUTTON.equals(v_IntentAction)) block, since it is also not needed.

Next, I would make sure that your media button actually works. Does it control other applications, like a music player? It may be that your media button is not Android-compliant or something.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanx for the answer. Ill make this changes and see what happens. Anyway the problem is not in the MediaButtonIntentReceiver class because i´ve already forced an intent using sendbroadcast() and the method onReceive works. So i think that the problem is with the intent ACTION_MEDIA_BUTTON. Actually I´m testing it on the emulator and pressing the sound buttons on it? Should it work this way? Thanks in advance! – Guilherme Gusman Jul 07 '11 at 11:49
  • 1
    @Guilherme Gusman: The emulator does not have a media button. – CommonsWare Jul 07 '11 at 11:56
  • By the way the question that you make in the end is really important. When i´m listening to some music the media button only raises or down the volume it doesn´t change the music or something. What i´m trying to do is that, make an program to change that behavior . But now i think this button, the volume button is not the media button... is that the problem? Is yes is there any intent related to this volume button? – Guilherme Gusman Jul 07 '11 at 12:15
  • 2
    @Guilherme Gusman: "But now i think this button, the volume button is not the media button... is that the problem?" -- correct. "Is yes is there any intent related to this volume button?" -- no. – CommonsWare Jul 07 '11 at 12:17
  • Ok thank you very much! haha So I can´t do what i´m trying to do... my phone wouldn´t support that! I´ll check your answer! – Guilherme Gusman Jul 07 '11 at 12:18