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
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.
BroadcastReceiver : 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information. Intent-Filter : Works with BroadcastReceiver and tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.
Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the 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. Intent filter must contain <action>
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.
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...
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