Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write sent sms to content://sms/sent table

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.

like image 848
sarath Avatar asked Sep 03 '12 07:09

sarath


1 Answers

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.

like image 123
vikki Avatar answered Nov 02 '22 04:11

vikki