JDS JDS - 2 months ago 26
Android Question

Android receiver for multiple actions?

Simple question - can I register a single BroadcastReceiver to multiple intent actions? Here's what I'm considering:

<receiver android:name=".myReceiver">
<intent-filter android:priority="1000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
</intent-filter>
</receiver>


So in myReceiver class' onReceive, could I check
intent.getAction()
for both
ACTION_MEDIA_BUTTON and ACTION_HEADSET_PLUG
?

Thanks.

Answer

I guess you can have multiple s each one having its action element.

<receiver android:name=".myReceiver">
     <intent-filter android:priority="1000000">
         <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
     </intent-filter>

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

And then check the Intent's action in the onReceive of the receiver.