Posts Tagged sending sms using implicit intent

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.

, , , , , , , , , , ,

10 Comments