Restore Sms : create thread if not exists

泄露秘密 提交于 2019-12-14 03:23:39

问题


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


回答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;
}


来源:https://stackoverflow.com/questions/30260877/restore-sms-create-thread-if-not-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!