Android Intent

In android, Intent is a messaging object which is used to request an action from another app component such as activities, services, broadcast receivers, and content providers.

Generally, in android, Intents will help us to maintain the communication between app components from the same application as well as with the components of other applications.

In android, Intents are the objects of android.content.Intent types and intents are mainly useful to perform the following things.

ComponentDescription
Starting an ActivityBy sending an Intent object to startActivity() method we can start a new Activity or existing Activity to perform required things.
Starting a ServiceBy sending an Intent object to startService() method we can start a new Service or send required instructions to an existing Service.
Delivering a BroadcastBy sending an Intent object to sendBroadcast() method we can deliver our messages to other app broadcast receivers.

Building an Intent Object

Generally, in android Intent object contains the information required to determine which component to start and the information about the action to be performed by the recipient component.

The Intent object in android is having following characteristics to help the android system to understand which component should start.

Passive data structure to hold an abstract description of an operation to be performed.

Intent constructor takes two arguments

  • Action
  • Entity

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));

  • This data could be a webpage (http) or a telephone number(tel), as long as there is an application registered to process this type of Intent, Android will find it and invoke it.

Component Name

It defines the name of the component to start and by using the component name android system will deliver intent to the specific app component defined by the component name. In case if we didn’t define component name then the android system will decide which component should receive intent based on other intent information such as action, data, etc.

In android, we can specify the component name for intent by using a fully qualified class name of the target component and package name, for example, com.tutlane.sampleActivity. We can set the component name by using setComponent(), setClass(), setClassName() or by using the Intent constructor.

Action

It defines the name of the action to be performed to start an activity. The following are some of the common actions to start an activity.

ActionDescription
ACTION_VIEWWe can use this action in intent with startActivity(), when we have information that activity can show to the user.
ACTION_SENDWe can use this action in intent with startActivity(), when we have some data that the user can share through another app such as an email app, social sharing app.

We can specify the action name of intent by using setAction() or with an Intent constructor.

Data

It specifies a type of data to an intent filter. When we create an intent, it’s important to specify the type of data (MIME type) in addition to its URI. By specifying a MIME type of data, it helps the android system to decide which is the best component to receive our intent.

Category

Generally, the android category is optional for intents and it specifies the additional information about the type of component that should handle an intent.

We can specify a category for intent by using addCategory().

The above properties (Component Name, Action, Data, and Category) will represent the characteristics of an intent. By using these properties, the android system will easily decide which app component to start.

Android Intent Types

There are two types of intents available in android, those are Implicit Intents and Explicit Intents.

Explicit Intents

  • Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.
  • Start Activity
  • Services
  • Broadcast
// explicit intent by specifying its class name
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);

Implicit Intents

  • Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
  • Dial Call
  • Map Location
  • Open WebPage
Intent read = new Intent();
read.setAction(android.content.Intent.ACTION_VIEW);
read.setData(Uri.parse("http://www.codingtimes.in"));
startActivity(read);

// or call somebody

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:555–555–5555"));
activity.startActivity(intent);

Intent Filter

Android OS uses intent filters to pinpoint the set of Activities, Services, and Broadcast receivers that can handle the Intent with help of specified set of action, categories, data scheme associated with an Intent.

<intent-filter> element is used in the manifest file to list down actionscategories and data types associated with any activity, service, or broadcast receiver.

<activity
    android:name=".SecondActivity"
    android:label="@string/app_name">

   <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <action android:name="com.example.My Application.LAUNCH" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="http" />
   </intent-filter>
</activity>

Pending Intent

Android has a variation on an intent called a pending intent. In this variation, Android allows a component to store an intent for future use in a location from which it can be invoked again. 

PendingIntent is basically an object that wraps another Intent object. Then it can be passed to a foreign application where you’re granting that app the right to perform the operation, i.e., execute the intent as if it were executed from your own app’s process (same permission and identity).

This is how we can use intents in android applications to invoke the required service or activity based on our requirements. 

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Android Services


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , SwiggyKFC


One comment

Leave a Reply

Your email address will not be published.