76
votes

So far I am using the following code to send SMS to another phone through my app.

Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + srcNumber)); 
                    intent.putExtra( "sms_body", message ); 
                    startActivity(intent);

However, this opens up the native messaging app, thereby putting my app's activity in the background. Is it possible to send the SMS directly without the native messaging app opening up? If yes, how?

6
Oh gr8 downvoter, pray tell me why? :) - SoulRayder
possible duplicate of Send SMS in android - Shabbir Dhangot
That doesn't explicitly mention the underlying issue of native messaging app opening up, though I agree both solutions are mentioned. This might be a useful material to those who require direct SMS sending approach, since I have explicitly highlighted the differences in the result obtained using the two approaches. - SoulRayder

6 Answers

130
votes

You can send messages from your application through this:

public void sendSMS(String phoneNo, String msg) {
    try {      
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);    
        Toast.makeText(getApplicationContext(), "Message Sent",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    } 
}

Also, you need to give SEND_SMS permission in AndroidManifest.xml to send a message

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

20
votes
public void sendLongSMS() {
    String phoneNumber = "0123456789";
    String message = "Hello World! Now we are going to demonstrate " + 
        "how to send a message with more than 160 characters from your Android application.";
    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message); 
    smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}

and don't forget to add

<uses-permission android:name="android.permission.SEND_SMS"/>
12
votes

Sending sms with permission request :

Add In manifest :

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

Add Java Function :

void sendSmsMsgFnc(String mblNumVar, String smsMsgVar)
{
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
    {
        try
        {
            SmsManager smsMgrVar = SmsManager.getDefault();
            smsMgrVar.sendTextMessage(mblNumVar, null, smsMsgVar, null, null);
            Toast.makeText(getApplicationContext(), "Message Sent",
                    Toast.LENGTH_LONG).show();
        }
        catch (Exception ErrVar)
        {
            Toast.makeText(getApplicationContext(),ErrVar.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ErrVar.printStackTrace();
        }
    }
    else
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 10);
        }
    }

}
7
votes

Yes, found the answer to my own question :)

Use the following code for the same :

 SmsManager sms = SmsManager.getDefault();
                     sms.sendTextMessage(srcNumber, null, message, null, null);

This requires the following permission to be declared on the android manifest xml.

  <uses-permission android:name="android.permission.SEND_SMS"/>
6
votes

A complete function to send sms with delivery report.

//sent sms
private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {

                case Activity.RESULT_OK:

                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:

                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:

                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {

            switch (getResultCode()) {

                case Activity.RESULT_OK:

                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;

                case Activity.RESULT_CANCELED:

                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
2
votes
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);