Understanding Intents and Intent Filters in Android – Explicit, Implicit, and Filters - Textnotes

Understanding Intents and Intent Filters in Android – Explicit, Implicit, and Filters


Learn about Intents and Intent Filters in Android. Understand how Explicit Intents are used to start specific activities within your app, how Implicit Intents interact with other apps, and how Intent Filters help declare the types of intents your activities can handle.

In Android, Intents are used for communication between components like Activities, Services, and Broadcast Receivers. They are the fundamental mechanism for starting activities, services, and communicating across app components. This tutorial covers both Explicit Intents, Implicit Intents, and how to use Intent Filters to define what types of intents your activities can handle.

1. Explicit Intent

An Explicit Intent is used to start a specific activity or service within your application. You explicitly specify the target component (Activity, Service, etc.) using the component name.

1.1 Example of Explicit Intent

An Explicit Intent is used when you know exactly which activity you want to start.

Example (Java):


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

In this example:

  1. MainActivity.this: Refers to the current activity.
  2. SecondActivity.class: Specifies the target activity to start.

1.2 Passing Data with Explicit Intent

You can also pass data between activities using putExtra(). This allows you to send data such as strings, integers, or any other primitive type.

Example (Java):


Intent explicitIntent = new Intent(MainActivity.this, SecondActivity.class);
explicitIntent.putExtra("key", "Hello from MainActivity");
startActivity(explicitIntent);

In SecondActivity, you can retrieve the passed data using getIntent().

Example (Java):


Intent intent = getIntent();
String message = intent.getStringExtra("key");
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

2. Implicit Intent

An Implicit Intent does not specify a particular component. Instead, it is used to request an action that can be handled by any app component capable of fulfilling that request. It allows apps to interact with other apps on the device, such as opening a webpage or sending an email.

2.1 Example of Implicit Intent

For instance, if you want to open a webpage in the default web browser, you can use an implicit intent with the ACTION_VIEW action.

Example (Java):


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

In this example:

  1. Intent.ACTION_VIEW: A standard action for viewing data.
  2. Uri.parse("https://www.example.com"): The URL you want to open.

2.2 Implicit Intent for Sending Email

You can also use an implicit intent to send an email.

Example (Java):


Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "example@example.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body of the email");
startActivity(Intent.createChooser(emailIntent, "Send Email"));

In this example:

  1. Intent.ACTION_SENDTO: Indicates you want to send a message using a specific protocol (in this case, mailto).
  2. Uri.fromParts("mailto", "example@example.com", null): Specifies the recipient’s email address.

3. Intent Filters

Intent Filters are declared in the AndroidManifest.xml file to specify what types of implicit intents an activity or service can handle. This is important when the system needs to know which components are capable of handling specific actions.

3.1 Declaring Intent Filters in AndroidManifest.xml

To declare an intent filter, you add an <intent-filter> element to the activity, service, or receiver in your AndroidManifest.xml file. This tells the Android system which types of implicit intents can be routed to your component.

Example (AndroidManifest.xml):


<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.ACTION_VIEW_DETAILS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

In this example:

  1. The activity SecondActivity declares it can handle the custom action com.example.ACTION_VIEW_DETAILS.
  2. android.intent.category.DEFAULT: A required category to make the intent filter work with implicit intents.

3.2 How Intent Filters Work

When you send an implicit intent, the system looks for components (activities, services, etc.) that can handle the specified action. It matches the intent with the declared filters in AndroidManifest.xml to find the appropriate component.

Example of sending an implicit intent that matches the above filter:


Intent intent = new Intent("com.example.ACTION_VIEW_DETAILS");
startActivity(intent);

In this case, the system will route the intent to SecondActivity because it has an intent-filter for com.example.ACTION_VIEW_DETAILS.

4. Intent Categories

In addition to actions, categories can be used to further narrow down the kinds of intents an activity can handle.

4.1 Common Categories:

  1. android.intent.category.DEFAULT: The default category, used in most cases.
  2. android.intent.category.BROWSABLE: Allows the activity to be started from a web browser.
  3. android.intent.category.LAUNCHER: Used to specify the activity that starts when the app is launched.

Example (AndroidManifest.xml):


<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>

This allows the MainActivity to handle intents with the action android.intent.action.VIEW and also be launched from a web browser.

5. Summary of Intents and Intent Filters

Type of IntentDescriptionUse Case
Explicit IntentStarts a specific component (activity, service, etc.) within the app.Navigating between activities in the same app.
Implicit IntentInitiates an action that can be handled by any app component (e.g., sending email, opening a URL).Interacting with other apps or system features.
Intent FiltersDeclare the types of intents an activity or service can handle in AndroidManifest.xml.Allows the system to know which component to route implicit intents to.