Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Understanding Intent-Filters

I would like to create an Intent-Filter, so that certain links will trigger the start of my application (see this stackoverflow-thread for example: How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS? )

While trying, I figured out, that I dont quite understand how Intents and Intent-Filters (defined in the Manifest.xml) actually work. What is the difference between the following:

<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.MAIN" /> 

or the following:

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

And what is actually the difference between category and action Intent-Filters. I read this page http://developer.android.com/reference/android/content/Intent.html but I still missing a basic understanding.

like image 789
Pascal Klein Avatar asked Apr 01 '11 15:04

Pascal Klein


People also ask

What do intent filters do Android?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.

What is the difference between intent and intent filter in Android?

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.

What is category in intent filter?

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).

Can an activity have multiple intent filters?

However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another. An <intent-filter> element in the manifest file lists actions as <action> subelements. For example: <intent-filter . . . >


2 Answers

Instead of looking at it from your app's point of view, flip it around and look at it from the Intent side.

When an Intent is created, the creator has no idea what apps are on the system to handle that Intent. But the creator does know what it wants to do (e.g., an app might want to let the user pick out a contact from somewhere on the device), and needs to reach out to other apps on the system to ask for what's desired.

To do this, Intents have several pieces of information attached to them. Among them are actions and categories.

The actions define in a general way the action the Intent wants to do, like VIEW a contact, PICK an image from the Gallery, etc.

The category is an additional piece of information that gives the Intent another way to differentiate itself. For example, when a link in the browser is clicked, the Intent that is created has the BROWSABLE category attached to it.

So, when the OS resolves the Intent, it will look for registered Activities or BroadcastReceivers that have an intent filter that includes all of pieces of information. If the Intent specifies the PICK action, Activities that do not have an intent-filter with the PICK action will be discarded from the list of candidates to handle the Intent.

In this way, the combined set of action, categories, type, and (possibly) scheme associated with an Intent serve to pinpoint the set of Activities that can handle the Intent. When you set up your intent-filter in your manifest, you are telling the OS which class of Intents you can handle.

like image 163
Brian Cooley Avatar answered Oct 09 '22 00:10

Brian Cooley


I had to examine the code of android.content.IntentFilter.matchCategories(Set<String> categories) to understand the matching of categories:

  1. Successful match, if your IntentFilter has categories and the Intent doesn't provide Categories
  2. Successful match, if your IntentFilter has all categories of the Intent. The filter also can have additional categories.
  3. No match, if your IntentFilter has no categories and the Intent has categories
  4. No match, if your IntentFilter has not the categories the Intent has

Especially #1 and #3 aren't obvious.

like image 45
OneWorld Avatar answered Oct 09 '22 00:10

OneWorld