Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Intent based on custom data

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>
like image 442
Jimmy Avatar asked May 10 '11 23:05

Jimmy


People also ask

What is intent filters explain intent filters with example?

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.

What is the difference between intent and intent filters?

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.

Can an activity have multiple intent filters?

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters.

Which component can you specify in an intent filter?

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.


1 Answers

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

like image 102
Maximus Avatar answered Sep 20 '22 14:09

Maximus