Android Tutorial - Android Intent

An intent is an action with its associated data.

Android uses intents to invoke components. The components in Android includes

  • activities (UI components),
  • services (background code),
  • broadcast receivers, and
  • content providers.

You can use intents to invoke external applications or internal components.

You can use intents to raise events so that others can respond in a manner similar to a publish-and-subscribe model.

You can use intents to raise alarms.


The following code shows how to use Intent to open an Activity.

Imagine you've the following activity:

public class BasicViewActivity extends Activity
    public void onCreate(Bundle savedInstanceState)

Then you register this activity in the manifest file, making it available for other applications to invoke.

<activity android:name=".BasicViewActivity"
          android:label="Basic View Tests">
   <action android:name="com.java2s.intent.action.ShowBasicView"/>
   <category android:name="android.intent.category.DEFAULT" />

To use an intent to invoke this BasicViewActivity:

public static void invokeMyApplication(Activity parentActivity)
   String actionName= "com.java2s.intent.action.ShowBasicView";
   Intent intent = new Intent(actionName);


The general convention for an action name is


BasicViewActivity can get the intent that invoked it.

class BasicViewActivity extends Activity
    public void onCreate(Bundle savedInstanceState)
        Intent intent = this.getIntent();
        if (intent == null)
            Log.d("test tag", "This activity is invoked without an intent");

Intent Composition

An intent has fours parts:

  • name,
  • data
  • type
  • category

In Android, intents usually come in pairs: action and data.

The action describes what is to be performed, such as editing an item, viewing the content of an item, and so on.

The data specifies what is affected, such as a person in the Contacts database.

The data is specified as an Uri object.

Some examples of action are as follows:


Some examples of data include the following:

  • tel:+9991231234
  • geo:37.999900,-122.999970
  • content://contacts

The action and data pair describes the operation to be performed.
For example, to dial a phone number, you would use the pair ACTION_DIAL/tel:+999234567.

To display a list of contacts stored in your phone, you use the pair ACTION_VIEW/content://contacts.

To pick a contact from the list of contacts, you use the pair ACTION_PICK/content://contacts.

Use Intent Categories

You can group your activities into categories by using the <category> element in the intent filter.

AndroidManifest.xml file:

          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android=""
              android:versionName="1.0" >

              <uses-sdk android:minSdkVersion="14" />
              <uses-permission android:name="android.permission.CALL_PHONE"/>
              <uses-permission android:name="android.permission.INTERNET"/>
                  android:label="@string/app_name" >
                      android:name=".IntentsActivity" >
                      <intent-filter >
                          <action android:name="android.intent.action.MAIN" />
                          <category android:name="android.intent.category.LAUNCHER" />

                  <activity android:name=".MyBrowserActivity"
                          <action android:name="android.intent.action.VIEW" />
                          <action android:name="com.java2s.MyBrowser" />
                          <category android:name="android.intent.category.DEFAULT" />
                          <category android:name="com.java2s.Apps" />
                          <data android:scheme="http" />



The following code will directly invoke the MyBrowerActivity activity:

Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse (""));
startActivity(Intent.createChooser(i, "Open URL using..."));

If you omit the addCategory() statement, the preceding code will still invoke the MyBrowerActivity activity because it will still match the default category android.intent.category.DEFAULT.

For the following code it does not match the category defined in the intent filter, therefore no activity will be launched:

The following code references category com.java2s.OtherApps and it does not match any category in the intent filter, so a run-time exception will be raised if you don't use the createChoose() method of the Intent class.

Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse (""));
//this category does not match any in the intent-filter
startActivity(Intent.createChooser(i, "Open URL using..."));