Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Understanding Implicit Intent in android

Understanding Implicit Intent in android

January 18th, 2011 Leave a comment Go to comments

In last post we had explained the concepts of intent understanding intent in android. Now, we will move on to a more interesting concept of Implicit Intents and Intent Filters. The code of this post can be found at end.

As described earlier, an implicit intent does not name a target component that should act upon the intent. Android resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?

Basically, an Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:

  • Action
  • Category
  • Data

So, Android compares the three (action, category and data) to something called Intent Filters that are declared by probable target components who are willing to accept Implicit Intent calls. i.e. Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.

So here are some important points to remember:

  1. Implicit Intents do not specify a target component.
  2. Components willing to receive implicit intents have to declare their ability to handle a specific intent by declaring intent filters.
  3. A component can declare any number of Intent Filters.
  4. There can be more than one component that declares the same Intent Filters and hence can respond to the same implicit intent. In that case, the user is presented both the component options and he can choose which one he wants to continue with.
  5. We can set priorities for the intent filters to ensure the order of responses.

There are 3 tests conducted in order to match an intent with intent filters:

  1. Action Test
  2. Category Test
  3. Data Test

Finally, we’ll look at declaring an implicit intent in one activity which will invoke one of the native activities of the platform by matching the intent filters declared by the same.

The ImplicitIntent Activity creates an implicit intent object contacts. This intent object’s component is not set. However, the action is set to android.content.intent.ACTION_VIEW and the data’s URI is set to People.CONTENT_URI.

Such an intent matches with the intent filter declared by the view contacts native activity.

So, when we run this application, it displays the native UI for viewing the existing contacts on the phone!

Here is the relevant piece of code for the same:

Button viewContacts = (Button)findViewById(R.id.ViewContacts);

viewContacts.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent contacts = new Intent();

contacts.setAction(android.content.Intent.ACTION_VIEW);

contacts.setData(People.CONTENT_URI);

startActivity(contacts);

}

});

In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.

Here are our Java code, ImplicitIntent.java:

package com.bogotobogo.implicitintent;

import android.app.Activity;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.os.Bundle;

import android.provider.Contacts.People;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ImplicitIntent extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ViewContacts();

}

private void ViewContacts() {

try {

Button viewContacts = (Button)findViewById(R.id.ViewContacts);

viewContacts.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent contacts = new Intent();

contacts.setAction(android.content.Intent.ACTION_VIEW);

contacts.setData(People.CONTENT_URI);

startActivity(contacts);

}

});

}catch (ActivityNotFoundException anfe) {

Log.e(“ViewContacts”,”Viewing of Contacts failed”, anfe);

}

}

}

If you want to know more about Intent go http://developer.android.com/reference/android/content/Intent.html

Create a new Project named MoveScreen.

Explore the project. In res/layout main.xml file is created during the project creation. Say main.xml file is for first screen and we need another xml file for second screen. Now we create another xml file named secondscreen.xml(lowercase letter only).  right click on layout layout>New>Other…

select Android XML File and click next

Give a name (for example) secondscreen.xml (lowercase letter only) and click Finish button.

Double click on secondscreen.xml file from the Package Explorer and click on layout tab. Add one TextView and a Button like bellow

Switch to “secondscreen.xml” and verify that your XML looks like the following.

<?xml version=“1.0″ encoding=“utf-8″?>

<LinearLayout  xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“vertical”>

<TextView android:id=“@+id/TextView01″ android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“SecondScreen”></TextView>

<Button android:id=“@+id/Button01″ android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“PreviousScreen”></Button>

</LinearLayout>

In the same way add a TextView and a Button within main.xml file.

Switch to “main.xml” and verify that your XML looks like the following.

<?xml version=“1.0″ encoding=“utf-8″?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent” >

<TextView   android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:text=“First Screen”/>

<Button android:id=“@+id/Button01″ android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“NextScreen”>

</Button></LinearLayout>

To move screen we use the intent like the following
Intent back = new Intent(SecondActivity.this,MoveScreen.class);

startActivity(back);

Our default class is MoveScreen class. It is created during the project creation.
MoveScreen.java:
package com.droidbd.movescreen;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MoveScreen extends Activity {

/** Called when the activity is first created. */

private Button next;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

next = (Button)findViewById(R.id.Button01);

next.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent i = new Intent(MoveScreen.this,SecondActivity.class);

startActivity(i);

}

});

}

}

Now we create an Activity class for second screen to handle secondscreen.xml file.The class name is SecondActivity.java

SecondActivity.java

package com.droidbd.movescreen;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class SecondActivity extends Activity{

private Button previousBtn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.secondscreen);
previousBtn = (Button)findViewById(R.id.Button01);

previousBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent back = new Intent(SecondActivity.this,MoveScreen.class);

startActivity(back);

}

});

}

}

All Android components that wish to be notified via intents should declare intent filters so that Android knows which intents should go to that component. So, we need to add intent-filter elements to our AndroidManifest.xml file.
Here we have created SecondActivity.java class.
We need to add the following lines to AndroidManifest.xml.

<activity android:name=“SecondActivity”></activity>

So, the manifest file should look like this:

AndroidManifest.xml

<?xml version=“1.0″ encoding=“utf-8″?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“com.droidbd.movescreen”

android:versionCode=“1″

android:versionName=“1.0″>

<application android:icon=“@drawable/icon” android:label=“@string/app_name”>

<activity android:name=“.MoveScreen”

android:label=“@string/app_name”>

<intent-filter>

<action android:name=“android.intent.action.MAIN” />

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

<activity android:name=“SecondActivity”></activity>

</application>

<uses-sdk android:minSdkVersion=“4″ />

</manifest>

Run

Run your application.You will see the following output.

Click on NextScreen button to see the next screen.

Click on PreviousScreen button to see the first screen.

Implicit Intent code sample

Be Sociable, Share!
  1. July 7th, 2013 at 02:54 | #1

    Hi there indeed it very explanatory in understanding of about implicit you also check the example of it at You can this one at
    Explicit Intent
    Implicit Intent

  2. Hurricane
  3. bunso001
    December 13th, 2011 at 23:07 | #3

    VERY HELPFUL , thank you ! :D D

  4. ARIF MAHMUD RANA
    November 13th, 2011 at 02:26 | #4

    Many many thanks. I was having trouble with implicit intent. But your tutorial help me a lot. Keep it up.

  5. Ashish Dwivedi
    August 7th, 2011 at 04:12 | #5

    Is there any idea how can i fire system intent on TabActivity ‘s Tabwidget. for example open native dialer in a Tabhost.

  1. January 18th, 2011 at 19:13 | #1
  2. January 18th, 2011 at 21:34 | #2

Get Adobe Flash playerPlugin by wpburn.com wordpress themes