Sending the SMS from android programmatically

CodingwithSaud
2 min readAug 24, 2021

--

How to send SMS from android using java programming

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);        }    }}

--

--

CodingwithSaud
CodingwithSaud

Written by CodingwithSaud

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

No responses yet