Android Intents and Intent Filters – Android Basics

Android Intents and Intent Filters

Android intent is an intention to perform an operation.  It is an abstract description of an operation to be performed. It can be used with startActivity() to launch an Activity, broadcastIntent() to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

Although intents facilitate communication between components in several ways, there are three fundamental use cases:

Starting an activity

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

Activity : StartActivity and StartActivityforResult.

Starting a Service

Service is a component that performs operations in the background without a user interface. Service : bindService / StartService

Delivering a broadcast

The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. To transfer data from one component to another (Custom objects can be transferred by implementing parcelable or Serializable interfaces).

Types of Intents:

Explicit Intent: It is an Intent which contains explicit information about a component which will be started using the intent, for example:

Implicit Intent: It is an Intent which doesn’t specify component information explicitly, but “Action” is used to find the component, for example:

Fields of Intent in Android:

Component:

It specifies the component to be started.

It can be set to class or fully qualified class name.

Context:

It is the context in which intent will be created.

For Activity: Context is “this”.

For ContentProvider: Context is getContext().

For Service: Context is getApplicationContext().

For BroadcastReceiver: Context will be passed as a parameter to onRecieve() method.

Action:

It is a String Constant which defines unique action performed by the intent (unique by its package name).

Category:

It is more information about an action.

By default, category is set to “android.intent.category.DEFAULT“.

Data:

It is a type of URI that supported in the component.

Example: Browser supports “http” URI, Telephone supports “tel” URI.

Context and Component are used for Explicit Intent and remaining fields are used for Implicit intent.

Intent Resolution:

To resolute intent Action, Data and Category are used for Implicit and Component is used for Explicit intent.

Resolution of Explicit Intent

Explicit intent can be resolved by Component name by creating an Object of required Class from required Package.

Resolution of Implicit Intent

To start a component with Implicit Intent it has to be registered with intent-filter.

Intent-filter filters intent based on following criteria:

1. If action/category/data is missing nothing will be checked.

2. If a category is set to default it will not be checked.

3. If intents action/category/data is not matching with intent-filters action/category/data, the intent is discarded.

4. If all parameters match the object of the component is created and launched.

Sticky Intent:

When we have to live the intent, an object of intent is live called Sticky-Intent.

      • Generally, OS use it at the situation where we have to broadcast every time.
      • We will not create every time object of intent by the use of Sticky-Intent.

Example: BatteryChanged, LocationChanged.

For this, we have not used any permission. It is only a feature i.e. managed by OS.

Team RCV Academy

About Team RCV Academy

RCV Academy Team is a group of professionals working in various industries and contributing to tutorials on the website and other channels.

View all posts by Team RCV Academy