Intents in Android

Have you ever wondered how a new Activity opens when we click on some button, suppose the settings button to show the Settings screen in any app? How does the app opens up when we click on its notification? How do we get Low battery alert in our mobile? All these things are possible because of Intent in Android.

An Intent is a messaging object that you can use to request an action from an app component. An Intent is basically an intention to do an action. It's a way to communicate between Android components to request an action from a component, by different components.

It's like a message that Android listens for and then react accordingly by identifying and invoking the app's appropriate component (like an Activity, Service, Content Provider, etc.). It can be within that same app or some other app as well.

If multiple apps are capable of responding to the message then Android provides the user with a list of those apps from which a choice can be made.


Uses of Intent in Android

There are three fundamental uses of intents:

1. To start 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 along.


2. To start a Service

A Service is a component that performs operations in the background and does not have a user interface. You can start a service to perform a one-time operation(such as downloading a file) by passing an Intent to startService(). The Intent describes which service to start and carries any necessary data.


3. To deliver a Broadcast

A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast() or sendOrderedBroadcast().


Types of Intents

In Android, there are two types of Intents:

  1. Explicit Intents
  2. Implicit Intents

Explicit Intents

When you explicitly define which Android component should be opened on some user action, then you use explicit intents. You generally use an explicit intent to start a new component in your own app, because you know which exact activity or service you want to start. For example, you can start a new activity in response to a user action or start a service to download a file in the background.


Create an Explicit Intent

To create an explicit intent,

  1. You need to make an Intent object. The constructor of the Explicit Intent's object needs two parameters as follows:

    Context c: This represents the object of the Activity from where you are calling the intent.

    Java file name: This represents the name of the java file of the Activity you want to open.

    Note: You need to mention the java file name with .class extension

    Intent i  = new Intent(this, MyJavaFile.class);
  2. Call startActivity() method and pass the intent's object as the parameter. This method navigates to the java file mentioned in the Intent's object.
    startActivity(i);
  3. If you need to pass some information or data to the new Activity you are calling, you can do this by calling putExtra() method before the startActivity() method. This method accepts key-value pair as its parameter.
    i.putExtra("key1", "I am value1");
    i.putExtra("key2", "I am value2");
    startActivity(i);

    Note: To receive the data in the new Activity and use it accordingly, you need to call the getIntent() method and then getStringExtra() method in the java class of the Activity you want to open through explicit intent. getStringExtra() method takes the key as the parameter.

    String a = getIntent().getStringExtra("key1");

    Doing this, stores the value stored at key1 into the string variable a.


Implicit Intent

When you just have to tell what action you want to perform without worrying which component will perform it, then you can use implicit intent.

Implicit intents do not name a specific component to perform a particular action, but instead it declares a general action to be performed, which allows any component, even from another app to handle it.

For example, if you want to show a specific location of the user on a map, you can use an implicit intent to pass the coordinates through the intent and then any other app, which is capable of showing the coordinates on a map will accept that intent.


Create an Implicit Intent

To create an implicit intent,

  1. You need to make an Intent object. The constructor of the Implicit Intent's object needs a type of action you want to perform.

    An action is a string that specifies the generic action to be performed. The action largely determines how the rest of the intent is structured, particularly the information that is contained as data and extras in the intent object. For example,

    ACTION_VIEW: This action is used when you have some information that an activity can show to the user, such as a photo to view in a Gallery app, or an address to view in a Map app.

    ACTION_SEND: This action is used when you have some data that the user can share through another app, such as an Email app or some Social Networking app.

    Note: You can specify your own actions for getting used by intents within your own app (or for getting used by other apps to invoke components in your app), but you usually specify action constants defined by the Intent class or other framework classes.

    Intent i = new Intent(Intent.ACTION_VIEW);
  2. You need to provide some data for the action to be performed. Data is typically expressed as a URI(Uniform Resource Identifier) which provides data to the other app so that any other app which is capable of handling the URI data can perform the desired action. For example, if you want to open a website through your app, you can pass the Uri data using setData() method as follows:
    i.setData(Uri.parse("http://www.google.co.in"));
  3. Call startActivity() method in the end with the intent object as the parameter.
    startActivity(i);