I want to broadcast intent with custom-data and only the receiver that have this custom data should receive this intent, how this could be done ?
this how i broadcast the intent :
Intent intent = new Intent();
intent.setAction("com.example");
context.sendBroadcast(intent);
and i define the broadcast receiver as following :
<receiver android:name="com.test.myReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.example"></action>
</intent-filter>
</receiver>
An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.
An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.
To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters.
The intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond. Intent filters are declared in the Android manifest file.
Setup your myReceiver class basically how anmustangs posted.
public class myReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent intent) {
//Do something
}
}
You shouldn't have to check the Intent action because you've already filtered for it based on what you've included in your manifest, at least in this simple case. You could have other actions and filters in which case you'd need to have some kind of check on the received intent.
If you want the filter to filter on data, the sdk documentation on intent filters covers that. I'm not great with data types so any example I would give would be relatively poor. In any event, here is a link to the manifest intent filter page: http://developer.android.com/guide/topics/manifest/intent-filter-element.html
And the specific page for the data element; http://developer.android.com/guide/topics/manifest/data-element.html
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