Print Shortlink

Dive into Android development – VI

Let’s develop a simple Android application to perform dial-up and SMS operations. Let’s have two buttons “Call” and “SMS” as shown below. Clicking these buttons will perform the appropriate actions.

The resource layout file for the two buttons is given below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.13" >

    <Button
        android:id="@+id/callBtn"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="50dp"
        android:text="Call" >
    </Button>

    <Button
        android:id="@+id/smsBtn"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="120dp"
        android:text="SMS" >
    </Button>

</RelativeLayout>

Performing dial up and SMS operations involve use of Intents in Android. Intent is a simple class that encapsulates an action and data. For example Dial, Sms, Capture image are all actions that you want to perform. Wrap this information and associated data in a Intent object and launch an activity using it. Intent sends a message to the underlying Android system which triggers the necessary activity. So to dial a number you can use Intent.ACTION_DIAL as shown below.

Intent callIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:12345678"));
startActivity(callIntent);

The phone number and the action are wrapped in an Intent object and launched using startActivity method. This will automatically take you to the dialer screen.
To send a message you can use Intent.ACTION_VIEW with some additional information as shown below.

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body", "Hi!!!");
smsIntent.putExtra("address", "12345678");
startActivity(smsIntent);

You can set the intent type to the MIME TYPE “vnd.android-dir/mms-sms”, and also provide the addresses and the message body.

The complete code of the activity class is shown below.

public class DiveIntoAndroid6Activity extends Activity {
	private Button callBtn,smsBtn;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		callBtn = (Button)findViewById(R.id.callBtn);
		smsBtn = (Button)findViewById(R.id.smsBtn);

		callBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent callIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:12345678"));
				startActivity(callIntent);
			}
		});
		smsBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Intent smsIntent = new Intent(Intent.ACTION_VIEW);
				smsIntent.setType("vnd.android-dir/mms-sms");
				smsIntent.putExtra("sms_body", "Hi!!!");
				smsIntent.putExtra("address", "12345678");
				startActivity(smsIntent);
			}
		});
	}
}

Leave a Reply