Tutorials Android Sending SMS Message In Android

Sending SMS Message In Android

This tutorial explains to Sending SMS Message In Android using  SMS intent method or by using using SmsManager class. SMS intent method invokes pre-installed SMS apps.

In Android, we can send SMS from an application in two ways. One by using Intent method and other using SmsManager API. In intent method it uses device’s Built-in SMS application to send a SMS message.

1. Sending SMS using  SmsManager API

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("", null, "< message body>", null, null);

SmsManager require, SMS_SEND permission in your android mainfeast.

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

2. Sending SMS by invoking Built-in SMS application

Intent sendIntent = new Intent(Intent.ACTION_VIEW);

	sendIntent.putExtra("sms_body", “"); 
	sendIntent.setType("vnd.android-dir/mms-sms");
	startActivity(sendIntent);

Follow the complete example for both the method’s
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone Number : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/mobileNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter phone number"
        android:phoneNumber="true" >
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message Body: "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/smsBody"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="Enter message body"
        android:inputType="textMultiLine"
        android:lines="5" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="10" >

        <Button
            android:id="@+id/sendViaIntent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="SMS Intent" />

        <Button
            android:id="@+id/send"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="Send" />
    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

	private Button shareIntent;
	private Button send;
	private EditText phoneNo;
	private EditText messageBody;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		phoneNo = (EditText) findViewById(R.id.mobileNumber);
		messageBody = (EditText) findViewById(R.id.smsBody);

		send = (Button) findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String number = phoneNo.getText().toString();
				String sms = messageBody.getText().toString();

				try {
					SmsManager smsManager = SmsManager.getDefault();
					smsManager.sendTextMessage(number, null, sms, null, null);
					Toast.makeText(getApplicationContext(), "SMS Sent!",
							Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(),
							"SMS faild, please try again later!",
							Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		});

		shareIntent = (Button) findViewById(R.id.sendViaIntent);
		shareIntent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent sendIntent = new Intent(Intent.ACTION_VIEW);
					sendIntent.putExtra("sms_body", messageBody.getText().toString());
					sendIntent.setType("vnd.android-dir/mms-sms");
					startActivity(sendIntent);
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(),
							"SMS faild, please try again later!",
							Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

3.  Output

Android SeekBar
Android SeekBar Example Screenshot

4. Download Complete Source code from GitHub

Download Complete Source From Github

Total
0
Shares
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Raghav
Raghav
9 years ago

Very informative tutorial. My application requirement is, it should send periodic SMS automatically. How can i run a scheduler?

Steven Fernandes
Steven Fernandes
9 years ago

Thanks OP good reference

param
param
6 years ago

sms is not sending by smsmanager

Previous Post
Android Tutorials

Android Seekbar Example

Next Post
Android Tutorials

How To Send Email In Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
3
0
Would love your thoughts, please comment.x
()
x