Android Intent and Intent Filters

What is Intent in Android?

Intent and Intent Filters are used as a message-passing mechanism that works both within your application, and between applications or between different app components.

Use of Intent

You can use Intents to support interaction among any of the application components installed on an Android device, no matter which application they’re a part of. This turns your device from a platform containing a collection of independent components into a single interconnected system.

  • One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the class to load) or implicitly.
  • Intents can also be used to broadcast messages across the system.
  • Using Intents to propagate actions — even within the same application — is a fundamental Android design principle.
  • Intent and Intent Filters can be used to perform following actions in common ways:
    • Declare your intention that an Activity or Service be started to perform an action, usually with (or on) a particular piece of data
    • Broadcast that an event (or action) has occurred
    • Explicitly start a particular Service or Activity
    • Passing object or data from one activity to another activity

Types of Intent

There are two types of intents:

  • Explicit intents
    • This will specify, which application will satisfy the intent, by supplying either the target app’s package name or a fully-qualified component class name.
    • You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start.
    • For example, you might start a new activity within your app in response to a user action, or start a service to download a file in the background.
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
  • Implicit intents 
    • Using an implicit intent is useful when your app cannot perform the action, but other apps probably can and you’d like the user to pick which app to use.
    • For example, if you want to show the user a location on a map, dial a call, open a webpage. You can use an implicit intent to request that another capable app show a specified location on a map.
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:555-2368"));
startActivity(intent);

What are Intent Filters?

  • If an Intent is a request for an action to be performed on a set of data, how does Android know which application (and component) to use to service the request?
  • Intent Filters are used to register Activities, Services, and Broadcast Receivers as being capable of performing an action on a particular kind of data.
  • Intent Filters are also used to register Broadcast Receivers as being interested in Intents broadcasting a given action or event.
  • Using Intent Filters, application components announce that they can respond to action requests from any application installed on the device.

Below is the code sample mentioned in AndroidManifest.xml file.

Each intent filter is defined by an <intent-filter> element in the app’s manifest file, nested in the corresponding app component (such as an <activity> element). Inside the <intent-filter>, you can specify the type of intents to accept using one or more of these three elements:

<activity android:name="ShareActivity">
    <!-- 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>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>
  • <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 (schemehostportpath) 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.

An implicit intent is tested against a filter by comparing the intent to each of the three elements (action, data, category). To be delivered to the component, the intent must pass all three tests.

If it fails to match even one of them, the Android system won’t deliver the intent to the component. However, because a component may have multiple intent filters, an intent that does not pass through one of a component’s filters might make it through on another filter.

Related posts