Intents in Android

Intents in Android

The intent in literal terms means the purpose. The intent is to be focused on something. It is something you plan or mean to do.

The intent in Android is an object that contains information about some action that needs to be performed. It facilitates communication between different app components.

In this article, we will try to cover the following simply.

  • Use cases
  • Types of Intent
  • Building an Intent
  • Receiving intent
  • Conclusion

Use Cases

Now, let’s look into some of the use cases where intents come into the picture.

  • Launching an activity

We can launch an instance of an activity using intent object in the startActivity() or startActivityForResult() method. The intent object holds information about the activity to start along with any extra data that we may require in the next activity.

  • Starting a service

We can start a service using an intent object in the startService() or bindService() method.

  • Delivering a broadcast

We can deliver a broadcast using an intent object in the sendBroadcast() method or sendOrderedBroadcast() method.

Types of Intent

  • Explicit Intent

The intent in which we specify the fully classified class name of the component. For example- Launching a new activity in the same application or the intent with the specified package name.

  • Implicit Intent

The intent in which we declare the action that needs to be performed. It does not specify the component name. It opens up the space for other applications that can handle the action mentioned in the intent. We can provide extended information to the component using extras. For example- we can send the subject, body, etc of a mail in the extras.

Using a PendingIntent

PendingIntent is a wrapper around the Intent object. When we want to perform some action at a later point, pending intent come into the scene. It works like the permission given to some other app to run the code of your application at a later time.

Use cases -

  • When we want to perform some intent when the user takes some action on Notification.
  • When a user takes some action on App Widget.
  • When we want to perform some intent at a future time.

Example- AlarmManager. It can perform the action you specified in pending Intent at a later point.

Building an intent

The below properties define the best suitable options for any action specified.

  • Component name

If the component name is mentioned, the system directly opens up the specified component. If not mentioned, it looks for the options that can handle the action defined. This field is optional.

  • Action

Going by the definition of intent, it contains information about the action that needs to be performed. That makes the action compulsory.

Example- ACTION_CALL, ACTION_SEND, ACTION_VIEW, ACTION_EDIT etc.

  • Data

Data defines the URI of data and/or MIME type of the data. For different actions, there are different URIs. Mentioning the MIME type of data is useful in finding the suitable apps for the intent.

For example- If the action is

- ACTION_EDIT: the data should contain the URI of the document to edit

- ACTION_CALL: the data should contain tel

  • Category

It contains extra information about the type of component that can handle the intent.

For example- CATEGORY_BROWSABLE, CATEGORY_LAUNCHER

  • Extras

If there is some extra information that needs to go with the Intent, it goes in the Extras in the form of key-value pairs.

For example, when creating an intent to send an email with ACTION_SEND, you can specify the recipient with the EXTRA_EMAIL key, and specify the subject with the EXTRA_SUBJECT key.

  • Flags

Flag defines how to launch the activity and how to treat it after it is launched.

Receiving an implicit intent

We declare intent filters for the app components in the manifest file. Only if the intent filter is passed, the system decides to deliver the intent to any app component in your application.

- It mentions the action that this component can accept in the name attribute.

- It mentions the data that this component can accept using one or more attributes that specify various aspects of the data URI (scheme, host, port, path) and MIME type.

- It mentions the category that this component can accept in the name attribute.

References