9

What I need:
- Send SMS with no GUI interaction (choosing a client to send SMS is out)
- SMS has to be visible in a thread queried from "content://mms-sms/conversations/"+threadId

Currently I'm using SMSManager:

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

Is there any ways to do this across all the devices considering each of them has a different SMS app. Thanks in advance.

Nam Ngo
  • 2,093
  • 1
  • 23
  • 30

2 Answers2

14

Just figured it out, you can use ContentResolver to insert the SMS and remember to add permissions: "uses-permission android:name="android.permission.WRITE_SMS"

   ContentValues values = new ContentValues();
   values.put("address", phone);
   values.put("body", message);
   getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Nam Ngo
  • 2,093
  • 1
  • 23
  • 30
  • Does that actually work? I tried it, and if I use my own mobile number and send it from the same phone with the SIM, the sms appears in the sent folder, but it's never been sent. The SMS never arrives if I send it with above code to any other recipient / different phone. So for me it appears, only point 2) of your initial requirement is met, but not 1) - wondering why did this answer get 4 upvotes? It works for others? – Mathias Conradt Mar 11 '11 at 11:30
  • 1
    @Mathias: This works for me. You have to use this code in conjunction with sendTextMessage() in the question above. The code provided in this answer only adds the message to sent messages, it doesn't actually send the message. I also needed to add "android.permission.READ_SMS"-permissions in addition to WRITE_SMS for it to work. You also need to import android.content.ContentValues; import android.net.Uri; – alexteg Apr 04 '11 at 22:26
  • @alexteg - I got it working meanwhile, thanks for the reply though! – Mathias Conradt Apr 05 '11 at 04:31
  • @MathiasLin +1 for adding READ_SMS permission, only WRITE_SMS makes the code crash. – Khobaib Nov 15 '13 at 09:56
  • Remember that this is undocumented, unsupported techniques that Google has explicitly told developers not to use. The reason is as quoted by CommonsWare - "there is no single "inbuilt Messaging App" in Android. HTC Sense has one. MOTOBLUR has one. Other OEM layers (e.g., Sony Ericsson's Rachael) probably have theirs. Plus, there is the one that is part of the open source project and will appear on Google Experience devices (e.g., Nexus One)." So when we develop our own SMS app, we should maintain it's own database. – Khobaib Nov 15 '13 at 10:01
1

You need to use the following value:

values.put("thread_id", threadId);

And it will be associated with the thread.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87