Android Intent

An intent is an abstract description of an action expected to be performed. Android Intent reflects English meaning of word intent, determination to do something. It is kind of a black box containing set of instructions to perform an action like,

  • To start or launch an Activity
  • To broadcast an Intent
  • To start / bind a Service

An Android app or the OS will be listening for an Intent. Based on the information given in an Intent the action will be performed.

Intent Class

At low level, Intent is a java class that belongs to content package in Android (android.conent.Intent). Among all its attributes, couple of attributes are important knowing and they are,

  • Action – It holds the value corresponding to action to be performed.
  • Data – It hold the URI of the screen to perform the action stated above.

These attributes hold the values that are evaluated while mapping the intents with appropriate application component which may be an activity, a service or broadcast receivers.

Other attributes are

  • Category – under which category the action has to be performed by the intent. For example CATEGORY_LAUNCHER makes the intent action to be performed on top of any other actions.
  • Type – specification of the type of data. This will be automatically retrieved by the evaluation operation. On setting this category the evaluation will be disabled.
  • Component – class name of the component to which the intents are supposed to destined. The component will be matched by evaluating the intent attributes. On setting this attribute, the evaluation is not needed.
  • Extras – contains the additional details to be sent to perform the action through intents. It is varied based on the action specified. The function putExtra() is used to set extra information needed by the activity or service. This function holds key value pair for each additional information. For example, for a send email activity the putExtra function will include the arguments as,
    • putExtra(Intent.EXTRA_EMAIL , new String[]{“email@javapapers.com”});
Ads by Google

Intent Classification

Android Intent can be broadly classified into the following two categories,

  • Explicit Intent – This kind of intent will directly specify the component class name to be run.
  • Implicit Intent – This kind of intent will not directly specify the component name; But will have extra data to adequate enough to evaluate against which component this Intent is expected to be run.

Intent Filters

Each component of application such as activities, services and broadcast receiver has set of intent filters for each functions for which the components are responsible. These intent filters are specified in Android Manifest file. All the intents passing to the component will be filtered through these set of intent filters to capture the right component. In other case, if the component name is directly specified in the intent instantiation, then there is no need of getting through these intent filters.

Intent filters includes the action, data and many other optional attributes. The intents are mapped with the component using these attributes holed by the intent filter. Intent filters are specified in the Android manifest file using <intent-filter> tag. For example, the intent filters for main activity will be as follows.

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>.

Intent filters for a broadcast receiver can be specified to the system in two ways. It can be done by either in static or in dynamic manner. In static method, the intent filters are declared in Manifest file with <intent-filter> …</intent-filter> tags. But, in dynamic method, the intent filters are instantiated by the program code itself. The later can receive intent from other application where as the former can receive intent within application only.

Example: Android Intent and Intent Filters for a Broadcast Receiver Component

This example will write a message that is broadcast in Android log.

Download Source Code: Intent Example

Create a new Android application project to broadcast a content entered by the user. The content to be broadcasted will be taken from the user through a main Android activity component. On clicking the send button, this content will be send to the broadcast receiver component. For that the activity layout contains a content edit text field and a send button.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toAddress"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="44dp"
android:layout_marginTop="26dp"
android:text="Content" />

<EditText
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:ems="10"
android:inputType="textMultiLine" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/content"
android:layout_below="@+id/content"
android:layout_marginTop="26dp"
android:onClick="requestToSend"
android:text="Send" />

</RelativeLayout> 

On clicking the Send button the requestToSend() method will be invoked. In this method, the user input is read from the form layout and pass through an intent using sendBroadcast method. Before that, the intent has to be instantiated with the current and destination component. The entire code for the activity java class is shown below.

MainActivity.java

package com.javapapers.android.broadcastcontent;

import android.os.Bundle;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.javapapers.android.broadcastcontent.R;

public class MainActivity extends Activity {

EditText content;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void requestToSend(View view) {
content   = (EditText)findViewById(R.id.content);
Intent intent = new Intent(MainActivity.this, Receiver.class);
intent.putExtra("content", content.getText().toString());
sendBroadcast(intent);
}
} 

While invoking the in built sendBroadcast() method by passing intents as its argument, the onReceive callback method of broadcast Receiver component will be called. This method includes the following code.

@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
try{
Log.d(LOGCAT, content);
}
catch(Exception e) {
Log.d(LOGCAT, "Problem in receiving broadcast!");
}
}

It gets the value of content text field using getStringExtra method and strore it into a local variable. Using this variable the corresponding content will be written into the log finally. The complete code for the broadcast receiver component is shown below.

Receiver.java

package com.javapapers.android.broadcastcontent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Receiver extends BroadcastReceiver{
private static final String LOGCAT = null;
@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
try{
Log.d(LOGCAT, content);
}
catch(Exception e) {
Log.d(LOGCAT, "Problem in receiving broadcast!");
}
}

}

Before running the application the BroadcastReceiver component should be added to the android manifest file. It should be specified inside <application> tag like as other component. The code snippet for the specification of Broadcast receiver in the manifest file will be as follows.

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="Receiver" ></receiver>
</application>

As we discussed above, if any component is supposed to map with the name of the component directly, then there is no need of specifying any intent filters to those component. The receiver component has only the name attribute and has no intent filters. Why because, this receivers are mapped with the name of the receiver component named Receiver. This is done inside requestToSend() function of the MainActivity class like,

Intent intent = new Intent(MainActivity.this, Receiver.class);
sendBroadcast(intent);

If the intent is instantiated with the action or data like as,

Intent intent = new Intent(Intent.ACTION_SEND);

Then the receiver component should be declared with the intent filter inside manifest file. Then can only the receiver will respond to the Broadcast event triggered from other application component.

Input Screen

Output to Log


Download Source Code: Intent Example

As we can conclude that the Android intents are bundle of information needed to perform the required job; Intent filters are used to filter the unwanted intents which are not corresponding to the operations of specific component; and broadcast intents are used to pass information between two components or to communicate betwen two component.

This Android tutorial was added on 09/12/2012.

«

»

Comments on “Android Intent”

  1. i been very confused about implicit and explicit
    intents ,Thank you your tutorial helped in understanding intent concept very well

    • kushal

      1) Implicit Intent::
      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.

      2) Explicit Intent::

      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.

Your Comment