Restore Sms : create thread if not exists

2019-09-21 02:03发布

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 restored but they don't appears in the sms app...

I think I have to create a new thread (a new conversation).

ContentValues initialValues;
initialValues = new ContentValues();
initialValues.put("_id", talk.getId());
initialValues.put("recipient_ids", talk.getContact().getId());
context.getContentResolver().insert(Uri.parse("content://mms-sms/conversations?simple=true"), initialValues);

App crashes with error :

MmsSmsProvider does not support deletes, inserts, or updates for this URI.content://mms-sms/conversations?simple=true

标签: android sms
1条回答
forever°为你锁心
2楼-- · 2019-09-21 02:51

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;
}
查看更多
登录 后发表回答