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, sent
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.
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.