Intents and Intent Filters (Part 1)

Learn about intent objects and intent filters that Android apps use to pass messages between app components.

In Android, intents provide a powerful way for different app components to communicate with each other and interact with other apps. They can also provide messaging services between various app components. By using intents, an app can start new activities, broadcast messages to other apps, or access different types of data from different sources. Let’s explore intents and intent filters for the communication between various apps and within components of the same app.

Types of intents

The Android OS has two types of intents:

  • Explicit intents: We use these intents to start a system service or a component in the same app. They specify the name of the target app’s package or the name of the component class. For instance, following a user action, we can start a new activity within our app or start a system service in the background.

  • Implicit intents: We use these intents to specify an action to perform rather than specifying a component name. This permits components from other apps to handle such an intent. For instance, when we need to display a map location to the app user, we can use an implicit intent to request the apps that can show a map location.

In Android, the use of implicit intent to start a service isn’t safe because we aren’t sure which service is going to respond to the intent. Furthermore, services run in the background, and app users don’t know which service starts.

Note: For safety purposes, the Android OS doesn’t allow us to start any system service using an implicit intent.

We don’t declare intent filters for services and always use explicit intents to start system services.

Intent filters

Intent filters are expressions defined in the manifest file of our app. They specify the types of intents a component can receive. Each intent filter can include one or more actions, data types, and categories. The actions describe the action requested by the intent, such as "android.intent.action.VIEW" for viewing data. The data types describe the type of data expected by the component, such as "image/*" for images. The categories are used to group intents into logical categories, such as "android.intent.category.LAUNCHER" for activities that can be launched from the launcher.

We can allow other apps to start an activity in our app by declaring an intent filter for the activity in the manifest file of our app. If we don’t state any intent filter for an activity, we’re able to start it only with explicit intent. For implicit intents, the Android OS finds the appropriate app component by matching the contents of the intent to the intent filters declared in the manifest file of other apps on the same device. If multiple intent filters satisfy the intent, the OS lets the user choose the app by displaying a dialog.

Let’s discuss the components of an intent and declare the intent filters in the manifest file.

Components of an intent

The Android OS uses the information that the intent objects carry to decide which app components should handle the intent. Intents can also carry the information for the component that receives the intent to perform the action. The following is the key information an intent can have:

Get hands-on with 1200+ tech skills courses.