Implicit Intent

Implicit intents do not specify a target component. Here, the android system itself finds the best component for handling the intent. This is done by comparing the contents of Intent object with Intent Filters. The comparison to Intent Filter is done with three elements of intent object namely action, data and category.

Intent Filters describe the capability of the component (Activity, Service or Broadcast Receivers) to handle an implicit intent. Components without intent filters can’t receive implicit intents but can only handle explicit intents.

Here is a simple example using implicit intent for you:

I’m just explaining using my application how to view contacts, make a call and send sms using implicit intents.

This is how the application looks:

The XML Code is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" View Contacts " />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/phone"
android:layout_width="150dip"
android:layout_height="wrap_content" />
<Button
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" call " />
<Button
android:id="@+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
</LinearLayout>

You need to add these permissions in the Manifest

<uses-permission android:name=“android.permission.READ_CONTACTS”/>

<uses-permission android:name=“android.permission.CALL_PHONE”/>

<uses-permission android:name=“android.permission.SEND_SMS”/>

Contacts can be read using the following code:

Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);

Here, the action to be performed is viewing the contacts. And the data on which the viewing action is to be done is the contacts.

Similarly making a call and then sending messages to a particular number is performed.

The whole code is give below:

package com.intentdemo1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ImplicitintentdemoActivity extends Activity {
/** Called when the activity is first created. */
Button read,call,msg;
EditText phone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
read=(Button)findViewById(R.id.read);
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);
}
});
phone=(EditText)findViewById(R.id.phone);
call=(Button)findViewById(R.id.call);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent call1=new Intent();
call1.setAction(android.content.Intent.ACTION_CALL).setData(Uri.parse("tel:"+phone.getText()));
startActivity(call1);
}
});
msg=(Button)findViewById(R.id.sms);
msg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sms=new Intent();
sms.setAction(android.content.Intent.ACTION_VIEW);
sms.setData(Uri.parse("smsto:"+phone.getText().toString()));
startActivity(sms);
}
});
}
}

When View contacts button is clicked, this is how is the screen looks:

When Call button is clicked:

When send sms button is clicked:

 

Hope this article helped you in some way.

Advertisements

, , , , , , , , , , ,

  1. #1 by Ethan on November 10, 2012 - 8:34 am

    I have been searching for examples of implicit intents and only found how to open a URL in a browser. This is the first website with more useful examples.

    Thank you very much. You just made my day.

    • #2 by Deepthi G on November 22, 2012 - 4:39 pm

      Hey,
      I’m glad my post was useful.. 🙂 Keep Reading.:)

      • #3 by prateek on April 5, 2013 - 12:54 am

        100% useful mam thank u i and i will also start sharing

  2. #4 by venkatareddy on January 7, 2013 - 4:38 pm

    nice article..

  3. #5 by Sarath on February 25, 2013 - 9:57 pm

    Nalla explanation Deepthi 🙂 Thnx

  4. #6 by Rajkumar on August 17, 2013 - 12:46 pm

    Hi Deepthi,
    I would like to develop an application in android. The requirement is to scan a document using the camera and to convert the scanned data to a pdf file, so that it could be used as an attachment in mail.

    Please provide me some sample code… Thank you.

  5. #7 by Bhargavi on October 13, 2013 - 9:12 pm

    it was so useful.. Thanks

  6. #8 by nabila on June 4, 2014 - 3:09 pm

    Thanks a lot Deepthi G it was very useful ^_^

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: