Question

I've read lots of articles about intent filters and I really can't understand exactly what they do?

so please if anybody can explain to me with a clear example what is the role of intent filters exactly ?

thanks

Was it helpful?

Solution

I think it's well documented here. Summarizing, when (e.g.) you pass an intent to Context.startActivity, or Context.startService, or broadcast it with Context.sendBroadcast, etc, what activity or service (or, what set of broadcast receivers) is the intent delivered to?

Answer: if the intent is "explicit", i.e., it has the component name field set, then it's delivered to the target component it designates -- this typically happens in communication within an application.

Otherwise ("implicit" intent, as is typical for communication between applications), Android must determine the best activity or service (or set of receivers) on its own. How does Android do that? That's where filters come in, and I quote:

It does so by comparing the contents of the Intent object to intent filters, structures associated with components that can potentially receive intents. Filters advertise the capabilities of a component and delimit the intents it can handle. They open the component to the possibility of receiving implicit intents of the advertised type. If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.

The web page in question continues with many details and, at the end, a complete, simple example, and I think it would be redundant to copy and paste them here.

OTHER TIPS

Simply put, Intent filters are a way of telling the OS how to launch/communicate with the different activities/services/broadcast receivers in your app. So for example, if you want links that start with http://www.mycompany.com to lead people into your app, an intent filter is the way to accomplish that. Once its setup, anytime someone clicks on a link that starts with that (in any app) the user will be presented with the option to use your app to view that page. You've probably seen this with youtube urls. Likewise, if you want the 'share' link commonly seen in many apps to list your app, would use an intent filter to do that.

hope this helps...

An intent filter lets the system know what data or service requests a component can handle. A component might be an activity, service or broadcast receiver.

If you are writing an image viewer, you would add an intent filter (or several) to the manifest describing the images you can handle. If you are writing a file browser, you might package up the details of an image file in an intent, and the system would sift through intent filters until it found a best match to handle that image. The same goes for any type of data or service that might be passed from one component to the next.

Intent Filters is a way of telling OS that let me handle/provide these kind of Activities/Services

By adding given filter to Manifest tells OS that i can also handle Sms service and whenever you send sms it will show in a list or you can also explicitly use this as your sms service.

<intent-filter>

    <action android:name="android.intent.action.SENDTO" />

    <action android:name="com.example.code.SMS_INTENT" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:scheme="smsto" />

</intent-filter>

And to explicitly using this service call it like ...

 Intent intent = new Intent("com.example.code.SMS_INTENT", Uri.parse(uri));

 intent.putExtra("from", "code");

 startActivity(intent);

IntentFilters are used to declare a pattern of Intent attributes that the declaring component will respond to. You can specify with an IntentFilter that a given Activity, Service or BroadcastReceiver will respond to a combination of action, data mime type, scheme, path, etc. For example if you register an Activity with IntentFilter for ACTION_SEND with data type "text/plain", your Activity will be called each time the users wants to send some text.

intentFilters advertise the capabilities of a component and delimit the intents that can handle. an IntentFilter that a given Activity, Service or BroadcastReceiver will respond to a combination of action, data mime type, scheme, path, etc.

The intent by comparing the intent to intent filters based on three aspects:

1:- The intent action
2:- The intent data (both URI and data type)
3:- The intent category

action :
Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

data :
Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path, etc.) and MIME type.

category :
Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

For example, This activity handles "SEND" actions with text data.

<intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
</intent-filter>

Intent filter tells the android system for which intent or event the android components(activity,service ,broadcast receiver) should listen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top