80

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?

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • Check [this](http://stackoverflow.com/questions/7620150/can-i-automatically-send-sms-without-the-user-need-to-approve) – MysticMagicϡ Oct 11 '14 at 05:00
  • Oh gr8 downvoter, pray tell me why? :) – SoulRayder Oct 11 '14 at 05:16
  • possible duplicate of [Send SMS in android](http://stackoverflow.com/questions/4967448/send-sms-in-android) – Shabbir Dhangot Oct 11 '14 at 05:20
  • 2
    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 Oct 11 '14 at 05:23

5 Answers5

137

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" />

Anuj Raghuvanshi
  • 664
  • 1
  • 7
  • 24
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
  • Yea.. I figured this out. Thanks for the prompt reply though :) – SoulRayder Oct 11 '14 at 05:06
  • You can appect this answer and close this thread if this is what you are looking for – Himanshu Agarwal Oct 11 '14 at 05:09
  • Its not letting me do so yet :) I have to wait :) – SoulRayder Oct 11 '14 at 05:09
  • does this require the user to approve before sending message – Kishan Kumar May 14 '16 at 06:41
  • 9
    This only works for builds where the API target is older than 23. For newer builds, you need to jump through more hoops as Google changed how permissions work for "dangerous" permissions like SEND_SMS. Sigh. – SMBiggs Jun 16 '16 at 15:12
  • 1
    Does this also add the sent message to the default sms app (i.e. system database)? – Daniels Šatcs Apr 30 '18 at 19:02
  • 5
    Please note that if you are publishing your app on google play this is a Policy violation and will get your app suspended eventually. – MIKE PERETZ May 21 '18 at 11:24
  • @MIKEPERETZ Can you link to any official documentation that says it's a policy violation? I tried searching for it but I don't find anything to support that. – Magnus Sep 20 '18 at 16:33
  • tust me you won't find any information. google are very discrete in details about specifics if you read their policies. everything is broad. practically speaking i recommend every developer reading this: [link](https://www.medianama.com/2015/01/223-google-play-mistakes-app-suspension/) – MIKE PERETZ Sep 20 '18 at 18:12
  • 2
    @MIKEPERETZ is right, Please see this https://play.google.com/about/privacy-security-deception/permissions/. I received a mail for using SMS related permissions in app. – praveenb Nov 20 '18 at 11:24
  • 5
    @All It's an old answer and currently it's violation of Google Play Policy so kindly don't use above code. – Himanshu Agarwal Nov 20 '18 at 16:42
  • 5
    As of now accessing call log or sms is considered a policy violation without prompting the user for making your app as default sms/call log handler – MIKE PERETZ Nov 20 '18 at 19:03
22
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"/>
A. Badakhshan
  • 1,045
  • 7
  • 22
Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
  • No one has mentioned this very important concept of multi-parts sms messages for the limitation of a sms message, when it contains single-byte or multibyte-byte characters. Thank you so much. – Zonouzi Jan 11 '23 at 13:23
14

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

}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
8

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"/>
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
3
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
Dilavar Malek
  • 1,157
  • 11
  • 18