Intent and Intent Filters

Intent and Intent Filters in Android Studio

In this article you will learn all about Intent and Intent Filters in android studio. An Intent is the way to request an action from another app component in form of messaging object. For example, when you want to start activity, send message or share image.

The ways that intent communicate with app components.

There are several ways where the intent communicate with app components.

1- Starting Activities

In order to start activity in android you need to pass an intent to the method startActivity(). This intent should describe the activity that you want to start and also can carry additional data to this activity. Moreover if you want to get result from the target activity, you can call startActivityForResult() method. Then, you can get the result in the current activity (start activity) in form of separated Intent object in your activity’s onActivityResult() callback.

2- Starting Service

A Service is the component that can do operation in the background without user interface. Further more, you can start service that can do one time operation such as downloading a file by passing an Intent to startService() method. Moreover, If the service is designed with a client-server interface, you can bind to the service from another component by passing an Intent to bindService().

3- Starting a broadcast

A Broadcast is a message that any app can recieve. Moreover, android system delivers various broadcast for system event. For example, turning WIFI on and off, or starting device to charge. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast() or sendOrderedBroadcast().

Intent types

There are two main types of Intents.

Explicit Intents

With this type of intent you specify the target app, package or class name. Moreover you can use this type to start a component in your own app, as you know the class name of the activity or service you want to start. For example, when you start known activity or service in your app.

Implicit Intents

Actually, in this type of Intent, you do not specify the target name, but instead declare a general action to perform, which allows a component from another app to handle it. For Example, when you want to send text from your activity with mail, you must specify the action for this intent (ACTION_SEND). The following image shows how implicit intent starts activity in android.

How the implicit intent starts an activity in android system

When you want to implicit start Activity B from Activity A, first you create an intent with intent filter. Then cast this intent in the startActivity() method in Activity A. After that the android system searches through all apps that contain the match intent filter. When match found the android system starts the match activity by calling the oncreate() method of the target activity( Activity B) and passing the intent to it.

Declare Intent Filters

Intent filters are a powerful method to launch an activity based not only on an explicit request, but also an implicit one. For example, when you explicitly start Gmail App you tell the system to start Send Email Activity in this app. By contrast, when you use implicity intent you send request that tells the android system to start the activity that can do this task. Further more, you can use this feature by declaring an <intent-filter> attribute in the <activity> element.

Build an intent

In order to android system determines which app component to start, it uses the informations that carried by an intent object.

In order to build an intent you need the following informations:

Component name

The component name is considered the difference point between implicit and explicit intent. It is the name of component to start like activity, service or broadcast. Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category—described below). You can set the component name with setComponent()setClass()setClassName(), or with the Intent constructor.

Action

The action is the string that specifies the generic action to perform such as (such as view or pick). In the case of a broadcast intent, this is the action that took place and is being reported. You can specify your own actions to be used by intents within your app (or to invoke components in your app by other apps), but you normally specify constants of action specified by the Intent class or other framework classes. Here are some common actions for starting an activity:

ACTION_VIEW

ACTION_VIEW is used when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app.

ACTION_SEND

Also known as the share intent, and it used when you have some data that the user can share through another app, such as an email app or social sharing app.

If you define your own actions, be sure to include your app’s package name as a prefix, as shown in the following example:

Java Code

static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";

Kotlin Code

const val ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL"

List of Standard Activity Actions

  • ACTION_MAIN
  • ACTION_VIEW
  • ACTION_ATTACH_DATA
  • ACTION_EDIT
  • ACTION_PICK
  • ACTION_CHOOSER
  • ACTION_GET_CONTENT
  • ACTION_DIAL
  • ACTION_CALL
  • ACTION_SEND
  • ACTION_SENDTO
  • ACTION_ANSWER
  • ACTION_INSERT
  • ACTION_DELETE
  • ACTION_RUN
  • ACTION_SYNC
  • ACTION_PICK_ACTIVITY
  • ACTION_SEARCH
  • ACTION_WEB_SEARCH
  • ACTION_FACTORY_TEST

Standard Broadcast Actions

These are the current standard actions that Intent defines for receiving broadcasts (usually through Context#registerReceiver or a <receiver> tag in a manifest).

  • ACTION_TIME_TICK
  • ACTION_TIME_CHANGED
  • ACTION_TIMEZONE_CHANGED
  • ACTION_BOOT_COMPLETED
  • ACTION_PACKAGE_ADDED
  • ACTION_PACKAGE_CHANGED
  • ACTION_PACKAGE_REMOVED
  • ACTION_PACKAGE_RESTARTED
  • ACTION_PACKAGE_DATA_CLEARED
  • ACTION_PACKAGES_SUSPENDED
  • ACTION_PACKAGES_UNSUSPENDED
  • ACTION_UID_REMOVED
  • ACTION_BATTERY_CHANGED
  • ACTION_POWER_CONNECTED
  • ACTION_POWER_DISCONNECTED
  • ACTION_SHUTDOWN

Data

The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent’s action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit.

When constructing an intent, it is often important to specify the type of data (its MIME type) in addition to the URI. For example, an activity that is capable of displaying images might not be able to play an audio file, even though URI formats may be similar. Specifying the MIME type of your data will enable the Android system to find the best component for your function.

To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().

Category

When you need to specify the kind of the component that should handle the content, you need to add the category to the intent. Further more, you can add any number of categories to an intent, however, most intent do not require category. Examples:

CATEGORY_LAUNCHER

The activity is the initial activity of a task and is listed in the system’s application launcher.

CATEGORY_BROWSABLE

The target activity allows itself to be started by a web browser to display data referenced by a link, such as an image or an e-mail message.

Extras

All the above properties represent the characteristics of an intent. With it the Android system is able to resolve which app component it should start. An intent can also supply the following information as Extras. It is a Key-value pairs that carry additional information required to accomplish the requested action. Moreover you can use extras by using the method putExtra();. Also, You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras().

For example, when creating an intent to send an email with ACTION_SEND, you can specify the to recipient with the EXTRA_EMAIL key, and specify the subject with the EXTRA_SUBJECT key.

Flags

Flags are defined in the Intent class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it’s launched (for example, whether it belongs in the list of recent activities).

For more information, see the setFlags() method.


Example explicit intent

If you built a service in your app, named DownloadService, designed to download a file from the web, you can start it with the following code:

Java Code

// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

Kotlin Code

// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
val downloadIntent = Intent(this, DownloadService::class.java).apply {
    data = Uri.parse(fileUrl)
}
startService(downloadIntent)

Example explicit intent

if you built a service in your app, named DownloadService, designed to download a file from the web, you can start it with the following code:

Java

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

Kotlin

// Create the text message with a string
val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, textMessage)
    type = "text/plain"
}

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(packageManager) != null) {
    startActivity(sendIntent)
}

Example filters

To demonstrate some of the intent filter behaviors, here is an example from the manifest file of a social-sharing app:

<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

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

Thank You. You can read about activities in android studio here

Leave a Comment

Your email address will not be published. Required fields are marked *