What are Intent Filters exactly?

Mina Samy picture Mina Samy · Jul 3, 2010 · Viewed 25.2k times · Source

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

Answer

Alex Martelli picture Alex Martelli · Jul 3, 2010

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.