问题
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