Sending the SMS from android programmatically

How to send SMS from android using java programming

Photo by Daria Nepriakhina on Unsplash

Step 1: Add Permissions to the Manifest File and OnCreate Method of Activity. If you are dealing with fragment you must add it to OnCreateView Method.

Code Snippet for Manifest File

First you need to declare these variables on top of the activity or fragment class

private static final int REQUEST_SMS = 0;private static final int REQ_PICK_CONTACT = 2;private BroadcastReceiver sentStatusReceiver, deliveredStatusReceiver;

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

Code Snippet for OnCreate or OnCreateView method

if (ContextCompat.checkSelfPermission(getActivity(), SEND_SMS)        != PackageManager.PERMISSION_GRANTED){    ActivityCompat.requestPermissions(getActivity(), new String [] {SEND_SMS},            1);}

Step 2:

In the this step you need to implement onResume Method in activity or fragement and in this Method you need to write following code.

sentStatusReceiver=new BroadcastReceiver() {    @Override    public void onReceive(Context arg0, Intent arg1) {        String s = "Unknown Error";        switch (getResultCode()) {            case Activity.RESULT_OK:                s = "Message Sent Successfully !!";                break;            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:                s = "Generic Failure Error";                break;            case SmsManager.RESULT_ERROR_NO_SERVICE:                s = "Error : No Service Available";                break;            case SmsManager.RESULT_ERROR_NULL_PDU:                s = "Error : Null PDU";                break;            case SmsManager.RESULT_ERROR_RADIO_OFF:                s = "Error : Radio is off";                break;            default:                break;        }    }};deliveredStatusReceiver=new BroadcastReceiver() {    @Override    public void onReceive(Context arg0, Intent arg1) {        String s = "Message Not Delivered";        switch(getResultCode()) {            case Activity.RESULT_OK:                s = "Message Delivered Successfully";                break;            case Activity.RESULT_CANCELED:                break;        }    }};getActivity().registerReceiver(sentStatusReceiver, new IntentFilter("SMS_SENT"));getActivity().registerReceiver(deliveredStatusReceiver, new IntentFilter("SMS_DELIVERED"));

Step 3:

In this step you need to implement onPause Mehtod in activity or fragment and write the following code in it.

getActivity().unregisterReceiver(sentStatusReceiver);getActivity().unregisterReceiver(deliveredStatusReceiver);

Step 4:

In this step you need to create the following function your activity or a class

public void sendMySMS( String phone,String message) {    //Check if the phoneNumber is empty    if (phone.isEmpty()) {        Toast.makeText(getContext(), "Please Enter a Valid Phone Number", Toast.LENGTH_SHORT).show();    } else {        SmsManager sms = SmsManager.getDefault();        // if message length is too long messages are divided        List<String> messages = sms.divideMessage(message);        for (String msg : messages) {            PendingIntent sentIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent("SMS_SENT"), 0);            PendingIntent deliveredIntent = PendingIntent.getBroadcast(getContext(), 0, new Intent("SMS_DELIVERED"), 0);            sms.sendTextMessage(phone, null, msg, sentIntent, deliveredIntent);        }    }}

--

--

--

I’m a good husband, father and Senior Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How To Check RAM And CPU Usage In Laravel

What is Flutter and why use flutter for app development?

Answers (Part 1) — 7 Difficult Python Test Questions Many Couldn’t Solve In Time

Blogs: Week of 8 Mar — 14 Mar

Celebrating .NET 6 with CleanBlazor 🧼🧽🧹

How to add django Tinymce Text Editor to django

This week in iOS Development(API’s and JSON)

[TIL-4] Little’s law, Tuning the thread pool size

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingwithSaud

CodingwithSaud

I’m a good husband, father and Senior Developer.

More from Medium

Integration of Huawei Account Kit in Book Reading Android app (Kotlin)

Notes on Android Dev — Lecture 2 — Intro to Kotlin

Working with SELinux on Android

Creating Sticky Headers with ViewBinding and ListAdapter