11

I am working on an android sms application.I can send sms to single contact by using the following code.

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

Now I want to send sms to multicontacts.Some suggest to use loop.SO now I am using loops to send sms to multicontact.

After sending each sms I write those values to sent table.

 ContentValues values = new ContentValues();
    values.put("address", mobNo);
    values.put("body", msg);
    getContentResolver().insert(Uri.parse("content://sms/sent"), values); 

Every new address will create a new thread id. For example if my receiver's address is x, then thread id 1, for y thread id 2.And if I want to send sms to both x and y ,then how can I write in to sms/sent table. If I use Loop,then it won't create any new thread id, because send address x already have thread id 1 and y already have thread id 2.So messages will listed under thread id 1 and 2 never creates a new thread id.

I tried to manualy insert thread id by

values.put("thread_id", 33);

But then the messages under new thread id do not listed in default app but in my app.

Please help me friends

Edit:I tried using 0, and then reading the thread_id that was generated, then place the next sms with this thread_id, still doesn't works.

Aexyn
  • 1,212
  • 2
  • 13
  • 30
sarath
  • 3,181
  • 7
  • 36
  • 58
  • so you want a separate combined thread even if there is already a thread for 1 and 2? – nandeesh Sep 03 '12 at 08:01
  • @Nandesh..yes..Native app works like that – sarath Sep 03 '12 at 08:45
  • threads are exactly that, they are based on a single number, you cant define a new thread for 2 numbers. You will have to make your own app and have your own db if you want it your way – nandeesh Sep 03 '12 at 08:50
  • native app works like that...They create separate thread id for group conversation – sarath Sep 03 '12 at 08:55
  • @nandeesh..I changed my question.Now have a look on this – sarath Sep 04 '12 at 04:29
  • @sarath I don't think I can help you with this question, but you could try looking at the source code for the messaging application on github (cyanogenmod or aosp). It helped me once on Android App development. – jadkik94 Sep 04 '12 at 20:06
  • @nandeesh actually a new thread is created for grouped contacts, even if each contact may already have another thread for them. – vikki Sep 15 '12 at 06:46
  • @vikki...can plz explain a little more with code... – sarath Sep 18 '12 at 05:19

2 Answers2

13

You need to create a new thread_id manually, a normal contentResolver.insert(...) won't do for multiple recipient messages. To create the new thread_id you query the following uri

content://mms-sms/threadID

and to it append the necessary recipients so that finally it looks like this

content://mms-sms/threadID?recipient=9808&recipient=8808

So the full example would look like this. Say the recipients are 9808 and 8808

Uri threadIdUri = Uri.parse('content://mms-sms/threadID');
Uri.Builder builder = threadIdUri.buildUpon();
String[] recipients = {"9808","8808"};
for(String recipient : recipients){
    builder.appendQueryParameter("recipient", recipient);
}
Uri uri = builder.build();

Now you can query uri in the normal way and this will give you a thread_id that you can use for the recipients specified, it will create a new id if one doesn't exist or return an existing one.

Long threadId = 0;
Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);
if (cursor != null) {
    try {
        if (cursor.moveToFirst()) {
            threadId = cursor.getLong(0);
            }
    } finally {
            cursor.close();
    }
}

Now use threadId to insert your SMSs.

A few things to note.

Do not use this threadId to insert single recipient messages for either 9908 or 8808, create a new thread_id for each or just do an insert without specifying the thread_id.

Also, be very careful with the builder.appendQueryParameter(...) part, make sure the key is recipient and not recipients, if you use recipients it will still work but you will always get the same thread_id and all your SMSs will end up in one thread.

vikki
  • 2,766
  • 1
  • 20
  • 26
  • @vikki..Thank u...let me check – sarath Sep 19 '12 at 09:06
  • @vikki....for all conversations it creates only one thread id..my key is recipient only...my code snippet is String[] recipients = to_nums.toArray(new String[to_nums.size()]);; for(String recipient : recipients){ builder.appendQueryParameter("recipient", recipient); } – sarath Sep 21 '12 at 10:06
  • I don't think you're using toArray properly, if to_nums is an array of strings then just iterate over that you don't need to convert it into a String[], – vikki Sep 21 '12 at 16:02
  • @Vikki..Have a look on this question pls..http://stackoverflow.com/questions/12577036/broadcast-receiver-stops-when-i-click-back-button – sarath Sep 25 '12 at 06:05
0

Looks like you should create a new thread for the group message and insert it into the new thread as well as the individual threads.

Ty Smith
  • 2,594
  • 2
  • 23
  • 29
  • Hi..I tried to put new thread id by values.put("thread_id", 33);..After that I can list the group conversation but the default sms app is not listing the conversation with new threadid. – sarath Sep 07 '12 at 04:29
  • Did you create the new thread using content resolver insert and assign its Id in the message? – Ty Smith Sep 07 '12 at 04:39
  • The following code i used to inset group sms..ContentValues values = new ContentValues(); values.put("address", mobNo); values.put("body", msg);values.put("thread_id", 33); getContentResolver().insert(Uri.parse("content://sms/sent"), values); – sarath Sep 07 '12 at 04:52
  • Yes, however a thread row should be inserted separately and it's id retrieved and inserted as the thread Id. You should have two inserts. – Ty Smith Sep 07 '12 at 06:22
  • Hi i did separately .And I can view the new conversation.But the default sms app not showing the conversation..ContentValues values = new ContentValues(); values.put("address", phoneNumber); values.put("body", message); values.put("thread_id", 99); getContentResolver().insert( Uri.parse("content://sms/sent"), values);. Twice i did – sarath Sep 07 '12 at 06:37
  • Take a look at this, has more info on working with SMS uris. http://stackoverflow.com/questions/2584058/android-querying-the-sms-contentprovider – Ty Smith Sep 09 '12 at 17:56
  • Ya..actually I used those links when I start my application..But for insert multi messgae for multicontacts i didnot get – sarath Sep 10 '12 at 04:02