Send SMS By Starting Your Activity

This example explains how you can send sms by starting your activity.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SendSMSExample.

2.) Write following into main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SendSMSExampleActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

 

3.) Add following permissions to your manifest file:

 

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.SEND_RESPOND_VIA_MESSAGE"/>

 

4.) Run for output.
Steps:

1.) Create a project named SendSMSExample and set the information as stated in the image.

Build Target: Android 4.4
Application Name: SendSMSExample
Package Name: com.example.SendSMSExample
Activity Name: SendSMSExampleActivity

sendsms1

2.) Open SendSMSExampleActivity.java file and write following code there:

package com.example.sendsmsexample;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;

public class SendSMSExampleActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri uri = Uri.parse("0018042636906");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "SMS text");
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send_smsexample, menu);
return true;
}
}

 

3.) Compile and build the project.

NOTE: Check on device for actual output.