How to send SMS in Android
Android Sample Codes

How to send SMS in Android

How to send SMS in Android tutorial show you basic example to send SMS message. In Android appication development, you can use either SmsManager API or device’s Built-in SMS application for sending SMS using android application.

How to send SMS in Android
      How to send SMS in Android

How to Send SMS in Android

Create a new Android project and name the project. I have already discussed how to create a new project in android in my previous tutorial. If you are a beginner in android programming follow this tutorial for installing android studio in your system. Before you proceed see the code for sending SMS using android code.

SMS API

String phonenumber=et1.getText().toString();//get receiver’s phone number
SmsManager sms=SmsManager.getDefault();
String msg=et2.getText().toString();
sms.sendTextMessage(phonenumber,null,msg,null,null);

Of course, you need SEND_SMS permission in your android application for sending sms. All the permissions needed by an application need to be specified in the AndroidManifest.xml file based on the android permission policy. For sending SMS in android, you have to set permission in android manifest file as follows.

<uses-permission android:name="android.permission.SEND_SMS" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.howtosendsmsinandroid">
    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET">

</uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

To send SMS, you have to first collect the receivers phone number and the text message. Following will be the content of res/layout/activity_main.xml file.

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" Enter the phone number of recipient"
        android:id="@+id/txt1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/number"
        android:hint="Enter Receiver's Phone Number"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Message"
        android:id="@+id/txt2"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="150px"
        android:id="@+id/Content"
        android:hint="Type Your message Here..."
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SEND SMS"
        android:id="@+id/btn"
        android:onClick="onmessage"/>
</LinearLayout>

To send an SMS in android using android application, you use the SmsManager class. In order to instantiate SmsManager  class call the getDefault() static method to obtain an SmsManager object. The sendTextMessage() method sends the SMS message. Following is the content of the modified main activity file java/com.example.howtosendsmsinandroid/MainActivity.java.

MainActivity.java
package com.example.howtosendsmsinandroid;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn;
    EditText et1,et2;
    private static final int Request_Read_Contacts=0;
    private static final int Request_Code_permission=2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=(EditText)findViewById(R.id.number);
        et2=(EditText)findViewById(R.id.Content);
        String cPermission= Manifest.permission.SEND_SMS;
        try{
            if (ActivityCompat.checkSelfPermission(this,cPermission)!= PackageManager.PERMISSION_GRANTED){

                ActivityCompat.requestPermissions(this,new String[]{cPermission},Request_Code_permission);

            }

        }
        catch(Exception e){
            e.printStackTrace();

        }
        String sPermission= Manifest.permission.INTERNET;
        try{
            if (ActivityCompat.checkSelfPermission(this,sPermission)!= PackageManager.PERMISSION_GRANTED){

                ActivityCompat.requestPermissions(this,new String[]{cPermission},Request_Code_permission);

            }

        }
        catch(Exception e){
            e.printStackTrace();

        }
    }
    public void onmessage(View v){
        String phonenumber=et1.getText().toString();
        SmsManager sms=SmsManager.getDefault();
        String msg=et2.getText().toString();
        sms.sendTextMessage(phonenumber,null,msg,null,null);
        Toast.makeText(getApplicationContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();
    }
}

Output

 Output of how to send sms in android tutorial is shown below.

How to send SMS in Android

Leave a Reply

Your email address will not be published. Required fields are marked *