1

I want to send an SMS, but not using the SmsManager class. I want to do it with the native SMS app which is there on an Android phone.

And here is the twist : I do NOT want to launch the native app while doing it. Is there some format of intent which can send an sms directly (given the sms-body and the phone number to send it to) via the native app (without the user having to click 'send').

I googled the same, but all results and responses simply launched the native sms, waiting for user to manually send the SMS. I have seen this being implemented in some apps like 'MightyText' and wish to implement in my app as well.

Please help !

devanshu_kaushik
  • 929
  • 11
  • 30

5 Answers5

14

Using the SmsManager will send the sms through the system but will not put it in the SMS content provider as was mentioned earlier. Hence any native messaging app will not see it.

To do so, you have to add it manually via the SMS content provider AFTER you send the message normally via SmsManager. Here's some sample code to help:

ContentValues values = new ContentValues();
values.put("address", "+12345678"); // phone number to send
values.put("date", System.currentTimeMillis()+""); 
values.put("read", "1"); // if you want to mark is as unread set to 0
values.put("type", "2"); // 2 means sent message
values.put("body", "This is my message!");

Uri uri = Uri.parse("content://sms/");
Uri rowUri = context.getContentResolver().insert(uri,values);

And that's all. After that you'll notice that it's added and the native messaging app displays it normally.

Please click "accept" answer if it works out for you.

wnafee
  • 2,126
  • 16
  • 23
  • 6
    Just wanted to add to wnafee's answer, this code also requires android.permissions.READ_SMS / android.permissions.WRITE_SMS to be added to the manifest, and the code works like a charm ! Thanks wnafee. – devanshu_kaushik Oct 24 '12 at 17:58
5

Ok, so you want to send a SMS, without using SmsManager and plus it should show up in your native SMS app list?

Firstly, you cannot send SMS bypassing SmsManager. If you look at the source code of all native messaging app for Samsung Galaxy Nexus, it will invoke SmsManager on button click.

so, the below piece of code as posted above is correct

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

Secondly, after sending the message, native apps put it into into SMS ContentProvider

follow this How to save SMS to inbox in android?

Word of caution is that now adding to this is not supported. So you may have to resort to a hack to add it into the sent box.

Community
  • 1
  • 1
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
0

If you only have ACTION_SENDTO, then, of course, any application that can send will pop up.

You need to add a filter for SMS

Community
  • 1
  • 1
rds
  • 26,253
  • 19
  • 107
  • 134
  • Thanks for posting the first constructive answer here. I was already using the URL filter. I tried filtering with content type as well, but it still pops-up the chooser. The thing is, I have seen an app which skips the chooser on my mobile to directly use the native sms app, so I am positive that it is possible to do that. – devanshu_kaushik Oct 15 '12 at 11:48
0

I have done something similar in a project I was working on. You need to use SmsManager

It would be something like this -

        SmsManager smsManager = SmsManager.getDefault();

        smsManager.sendTextMessage(phoneNumber, null, message, null, null);

You can use this to send an SMS programatically.

Ayrton Senna
  • 3,735
  • 5
  • 34
  • 52
  • Will it record my sms in the native app? – devanshu_kaushik Oct 21 '12 at 18:22
  • No, the sent message does not show up in the native app. There might be a way for that but I am not sure about it. – Ayrton Senna Oct 21 '12 at 18:49
  • And that is why I don't want to use the smsManager, it would have been fine if I was building my own UI for the sent messages, but it is out of scope for my app. Hence the first line of my question. – devanshu_kaushik Oct 21 '12 at 18:59
  • So I get a down vote for trying? Great way to get answers from people. – Ayrton Senna Oct 21 '12 at 19:28
  • 2
    I think you got a down vote for not reading the question (but you didn't get it from me) – David Wasser Oct 22 '12 at 15:31
  • Ayrton - As David said, the down vote was for not reading the question properly. It is necessary to maintain the quality of questions and answers at a forum like StackOverflow. My question got down voted too, and that made me realize I may have not framed it correctly, so I edited it and voila ! I got my answer. No need to take down votes personally. Cheer up :) – devanshu_kaushik Oct 24 '12 at 17:56
  • I understand that I should have read the question properly and the answer was not right. Thanks David and dev_android :) – Ayrton Senna Oct 24 '12 at 20:11
0

Try this:

String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();



try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
    Toast.makeText(getApplicationContext(), "SMS Sent!",
        Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(getApplicationContext(),
        "SMS faild, please try again later!",
        Toast.LENGTH_LONG).show();
    e.printStackTrace();
}
Ranveer
  • 6,683
  • 8
  • 45
  • 92
Arun
  • 184
  • 1
  • 9