Android Sending Email Example

0

In this article, I will show you how to send SMS in Android. We have two methods of sending SMS.

  1. In the first one, you explicitly depend on android.telephony.SmsManager to send SMS
  2. In the second method, you rely on the Android’s built-in SMS activity

Let’s get started with our example.

Main Screen

In the main screen, we support both the methods.
We have a text field to enter phone number, its inputType is set to phone.
We also have a text field to enter the message, its inputType is set to textMultiLine so that we can enter multiple lines.
There are two buttons. The first one is ‘Send SMS’ and second on ‘Built-in SMS’.
‘Send SMS’ sends SMS programatically relying on android.telephony.SmsManager. The second button ‘Built-in SMS’ depends on Android’s built-in SMS mechanism.

welcome.xml:

<LinearLayout 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:orientation="vertical"
    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="com.javarticles.android.DatePickerExample" >

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="18dp"
        android:text="@string/welcome"
        android:textColor="@color/welcome_text_color"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_phone_number"
        android:inputType="phone">
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/sms_text"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:inputType="textMultiLine" 
        android:hint="@string/enter_sms_message"/>

    <Button
        android:id="@+id/send_sms_btn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/send_sms_btn"
        android:onClick="sendSms" />

    <Button
        android:id="@+id/built_in_sms_btn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/builtin_sms_btn"
        android:onClick="builtinSMS" />

</LinearLayout>

SMS Permission

In order to send SMS, you need to have the SEND_SMS permission. If you don’t have the permission you will get the below error.
04-14 11:51:58.889: E/AndroidRuntime(1594): Caused by: java.lang.SecurityException: Sending SMS message: User 10071 does not have android.permission.SEND_SMS.

android_manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javarticles.android"
    android:versionCode="1"
    android:versionName="1.0">
	<uses-sdk
		android:minSdkVersion="14"
		android:maxSdkVersion="22"
		android:targetSdkVersion="22"/>
	<uses-permission android:name="android.permission.SEND_SMS" />
	<application
        android:icon="@drawable/ic_launcher_sandbox"
        android:label="@string/app_name"
        android:allowBackup="true">
        <activity android:name="com.javarticles.android.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Main Activity

When you click on ‘Send SMS’, method sendSms will be called. In order to send SMS, first we need to get an instance of android.telephony.SmsManager.

android.telephony.SmsManager  smsManager = android.telephony.SmsManager.getDefault();

Call smsManager.sendTextMessage to send SMS. You need to pass the phone number and the SMS text.
Method builtinSMS is called when you press ‘Built-in SMS’ button. We create an intent of action Intent.ACTION_VIEW. This is used to display the data to the user.

Intent openSmsViewIntent = new Intent(Intent.ACTION_VIEW);

Android knows based upon the specified “type” that it is meant for the built-in SMS Activity.

openSmsViewIntent.setType("vnd.android-dir/mms-sms");

The “address” and “sms_body” extras specify the phone number and the body of the message that will be filled in when the SMS Activity launches.

openSmsViewIntent.putExtra("sms_body", "Your message");
//un-comment below to display the phone number 
//openSmsViewIntent.putExtra("address", "123456");

Finally, start the SMS activity:

startActivity(openSmsViewIntent);

MainActivity:

package com.javarticles.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText phoneNumber;
	private EditText smsText;
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.welcome);
		phoneNumber = (EditText) findViewById(R.id.phone_number);
		smsText = (EditText) findViewById(R.id.sms_text);
	}
	
	public void sendSms(View view) {
		android.telephony.SmsManager  smsManager = android.telephony.SmsManager.getDefault();
		smsManager.sendTextMessage(phoneNumber.getText().toString(), null, 
				smsText.getText().toString(), null, null);
		Toast.makeText(this, "Sms sent to " + phoneNumber.getText().toString(), Toast.LENGTH_SHORT).show();
	}
	
	public void builtinSMS(View view) {
		Intent openSmsViewIntent = new Intent(Intent.ACTION_VIEW);
		openSmsViewIntent.setType("vnd.android-dir/mms-sms");
		openSmsViewIntent.putExtra("sms_body", "Your message");
		//un-comment below to display the phone number 
		//openSmsViewIntent.putExtra("address", "123456");
		startActivity(openSmsViewIntent);
	}
}

Run the application

Main screen

Main SMS Screen

Main SMS Screen

Send SMS

Send SMS

Send SMS

Built-in SMS

Built-in SMS Screen

Built-in SMS Screen

Download the source code

This was an example of How to send SMS in Android? You can download the source code here: sendSms.zip

Share.

Comments are closed.