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

Recursion for dummies

Know about Moz and Alexa Metrics

Why you should migrate from Bootstrap 3 to Bootstrap 4

Moving files (Dockers, Servers, Localhost,…)

Logging in Java

GOJEK Engineer AMA Session with Jason Ganub

Introducing the GUI for Tezster and the road ahead

Ansible roles for loadbalancer and apache webserver

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

Build AAB file and Sign key later with Jarsigner

Compilation of why Android 12 is not so good [with screenshots]

Data is Scrollable or not in WebView Android?

Setting up Mapbox SDK 10.1.0 in Android Studio (2021)