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):
In this example:
MainActivity.this: Refers to the current activity.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):
In SecondActivity, you can retrieve the passed data using getIntent().
Example (Java):
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):
In this example:
Intent.ACTION_VIEW: A standard action for viewing data.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):
In this example:
Intent.ACTION_SENDTO: Indicates you want to send a message using a specific protocol (in this case, mailto).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):
In this example:
- The activity
SecondActivitydeclares it can handle the custom actioncom.example.ACTION_VIEW_DETAILS. 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:
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:
android.intent.category.DEFAULT: The default category, used in most cases.android.intent.category.BROWSABLE: Allows the activity to be started from a web browser.android.intent.category.LAUNCHER: Used to specify the activity that starts when the app is launched.
Example (AndroidManifest.xml):
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 Intent | Starts a specific component (activity, service, etc.) within the app. | Navigating between activities in the same app. |
| Implicit Intent | Initiates 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 Filters | Declare 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. |