Android Intents

In this article, we will explore the concept of Android Intents in detail, and discuss their importance in Android development.

In Android, an intent is a messaging object that requests an action from another component, either within the same application or in another app.

Intents can be used to launch Activities, Services, and Broadcast Receivers, as well as to pass data among them.

Android Intent consists of an action, a data type, and optionally, data and category information.



Android Intent Actions

As you create an Intent, you must specify an action that describes the operation you want to perform, such as “view“, “edit“, or “send“.

The action informs the Android system what kind of component should handle the Intent.

For instance, if you have an Intent with the “ACTION_VIEW” action, it means that you want to display data to the user, like a web page, image, or video.

Android has some predefined actions, but you can also create custom actions for your app.

Here are the some of the most commonly used actions:

ActionsOverview
ACTION_VIEWDisplay data to the user, such as a web page, image, or video.
ACTION_EDITEdit an existing data item.
ACTION_SENDSend data to another app or service.
ACTION_DIALInitiate a phone call.
ACTION_CALLCall a specific phone number.
ACTION_WEB_SEARCHPerform a web search.
ACTION_PICKPick a data item, such as a contact or photo, from a list.
ACTION_GET_CONTENTRetrieve data from another app, such as a photo or document.
ACTION_BATTERY_CHANGEDBroadcast action indicating that the battery level has changed.
ACTION_POWER_CONNECTEDBroadcast action indicating that the device has been connected to a power source.
ACTION_POWER_DISCONNECTEDBroadcast action indicating that the device has been disconnected from a power source.

Android Intent Data

The data component is where you specify what the Intent will operate on, such as a URI, file path, or content provider URI.

It is used to identify the particular data that the Intent should act upon.

For example, if you create an Intent with the “ACTION_VIEW” action and a “data” URI of a web page, it will open the web page in the default web browser.


Android Intent Category

The category component is optional and can be used to further describe the intent’s purpose, like “default” or “alternative“.

Categories are strings that indicate additional information about the Intent.

For instance, if you create an Intent with the “ACTION_VIEW” action and the “CATEGORY_BROWSABLE” category, it can be handled by a web browser capable of browsing the web.

Some of the most commonly used categories are defined below:

CategoriesOverview
CATEGORY_DEFAULTIndicates a regular, default action should be taken for this intent.
CATEGORY_BROWSABLEIndicates that the intent can be handled by a web browser.
CATEGORY_LAUNCHERThis category indicates that the intent is an activity that should be displayed in the launcher.
CATEGORY_ALTERNATIVEIn this category, the intent represents an alternative action to the original intent.
CATEGORY_TABThis category implies that the activity represents a tab in a multi-tab activity.
CATEGORY_VOICEVoice recognition can be used to handle this intent.
CATEGORY_APP_EMAILDefines that the intent must be handled by an email client.
CATEGORY_APP_MESSAGINGThis category designates that the intent should be handled by a messaging client.
CATEGORY_APP_CONTACTSSpecifies that the intent should be handled by a contacts app.
CATEGORY_APP_CALENDARDesignates that the intent should be handled by a calendar app.

The action, data, and category components of an Intent are optional. Intents can contain one or more components, and each component may have its own data, action, and category attributes.

An intent can also include extra data in the form of key-value pairs, which can be used to pass additional information between components.

In addition to the action, data, and category, an intent in Android can also contain extras and flags.


Android Intent Extras

Extras in Android are key-value pairs that allow additional data to be passed between components.

The key is a string and the value can be any basic data type.

Extras are commonly used to pass data to an activity when it is launched, for instance, an intent to start an activity displaying a message can contain an extra with the key “message” and the value “Hello, World!“.


Android Intent Flags

Flags in Android Intent are integer values that are used to modify the behavior of an Intent.

By using bitwise OR, multiple flags can be combined to set more than one flag at a time.

Flags play a crucial role in controlling how an Activity is launched or how an Intent is handled.

Some of the flags available are:

FlagsOverview
FLAG_ACTIVITY_NEW_TASKCreates a new task for the activity if no task exists to handle it.
FLAG_ACTIVITY_CLEAR_TASKClears any existing tasks for the activity before starting it in a new task.
FLAG_ACTIVITY_SINGLE_TOPIf the activity is already at the top of the task stack, then it will not be started again and the onNewIntent() method will be called instead.
FLAG_ACTIVITY_CLEAR_TOPClears any existing activities on top of the activity before starting it.
FLAG_ACTIVITY_NO_HISTORYEnsures that the activity will not be stored in the activity stack, so that when the user presses the back button, the activity will not be shown.
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTSExcludes the activity from the list of recently used apps.
FLAG_ACTIVITY_BROUGHT_TO_FRONTBrings the activity to the front of the task stack, regardless of whether it was already running or not.
FLAG_ACTIVITY_FORWARD_RESULTAllows the activity to pass a result code back to the activity that started it.
FLAG_ACTIVITY_PREVIOUS_IS_TOPIf the activity being started is already running, it will be brought to the front of the stack instead of starting a new instance.
FLAG_ACTIVITY_REORDER_TO_FRONTBrings the activity to the front of the task stack, but does not create a new instance if it is already running.

Types of Intents

There are two types of intents in Android: explicit intents and implicit intents.

Explicit intents

An explicit intent starts a specific component within the same application.

For instance, you can use it to launch a specific activity within the app.

Example code for creating an explicit Intent:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

Implicit intents

A component outside the current application is started with an implicit intent.

For example, it can be used to launch a web browser to view a specific URL.

The following code creates an implicit intent:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
startActivity(intent);

Android Intent Filters

Android Intent filters define the capabilities of components and the types of intents they can respond to.

Components can be configured to handle specific actions, data types, and categories.

The intent filter allows an application to make its components available to other applications and allow them to interact with them.

Here is an example of how to create an intent filter:

<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>

In this example, we are creating an intent filter for the SecondActivity that specifies that it can handle “http” and “https” data schemes.

Here is an example of intent:

Launching an activity with extras:

Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("key", "value");
startActivity(intent);

Opening a web page:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
startActivity(intent);

Sending an email:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "[email protected]", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Message body");
startActivity(Intent.createChooser(intent, "Send email"));

Making a phone call:

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567890"));
startActivity(intent);

Opening a map location:

Uri gmmIntentUri = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent intent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);

These are just a few examples of how intents can be used on Android. From sending messages to placing phone calls, they can be used for a variety of tasks.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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