Skip to content Skip to sidebar Skip to footer

Restore Sms : Create Thread If Not Exists

I'm creating an Android app able to restore SMS from webservice. I insert SMS in existing conversations and it works fine. But, if the conversation does not exists, the sms are res

Solution 1:

This example give you a threadId, it will create a new id if the recipient doesn't exist otherwise it will return the existing threadId:

public static long getThreadId(Context context, String phoneNumber) {
    Uri threadIdUri = Uri.parse("content://mms-sms/threadID");
    Uri.Builder uriBuilder = threadIdUri.buildUpon();
    uriBuilder.appendQueryParameter("recipient", phoneNumber);
    Uri uri = uriBuilder.build();

    Cursor cursor = context.getContentResolver().query(uri, 
        new String[]{"_id"} /* projection */, 
        null /* selection */, 
        null /* selectionArgs */, 
        null /* order */);
    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                return cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }
    return 0;
}

Post a Comment for "Restore Sms : Create Thread If Not Exists"