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
?
There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.
As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.
The main difference in working between the static and dynamic receivers is that the static receivers will run if the application is running/not running. But the dynamic receivers will run if the application is running.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With