0

Here is what's inside the manifest(this is inside

<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter android:priority="2147483647">
    <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
        </receiver>

Main activity

private MediaButtonIntentReceiver receiver = new MediaButtonIntentReceiver();

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

And this is MediaButtonIntentReceiver.class

public class MediaButtonIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 


{

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

For some reason, whenever I launch my application and click my headset button I don't get any Toast message. Music starts playing though. I've tried searching for what I'm doing, but no luck for me.

Matt Smith
  • 965
  • 3
  • 13
  • 24

1 Answers1

0

not 100% sure if it makes a difference but in the declaration of your receiver you have a '.' before the function name, any time I have registered a receiver I haven't had that there and it's worked fine.

Do you know how far into the BroacastReceiver the code gets? for example could v_Event be null? or is v_Action definitely KeyEvent.ACTIONDOWN?

Cob50nm
  • 911
  • 2
  • 9
  • 27
  • Well I have looked further into this... tried different code. Tried to create an intent filter in the java part instead of the xml.. Nothing. I think this might be priority related, even though I set the priority as the maximum possible number, Apollo still kicks in. – Matt Smith Aug 06 '13 at 16:19
  • It looks like the priority you have declared in your xml is greater than the maximum allowable priority, try bringing it down to 1000, or if that doesn't work 999 because it might need to be `<1000` rather than `<=1000` – Cob50nm Aug 06 '13 at 16:36
  • I've removed the intent filter in the XML file before creating the one in java. I'm trying it on an emulator now. I think my problem is code-based. – Matt Smith Aug 06 '13 at 16:40
  • I've tried to fire the ACTION_MEDIA_BUTTON intent with a button and the intent filter actually works. Whenever I click the physical headset button though, Apollo kicks in. I don't know what's wrong. – Matt Smith Aug 06 '13 at 16:57
  • p.s. = priority is set to IntentFilter.SYSTEM_HIGH_PRIORITY. still nothing – Matt Smith Aug 06 '13 at 17:02
  • Not sure then, have you looked at any of these? http://stackoverflow.com/questions/6287116/android-registering-a-headset-button-click-with-broadcastreceiver http://stackoverflow.com/questions/9056814/how-do-i-intercept-button-presses-on-the-headset-in-android http://android-developers.blogspot.ie/2010/06/allowing-applications-to-play-nicer.html they all seem to deal with the same issues and have resolved them – Cob50nm Aug 06 '13 at 17:14