2015-05-15 18 views
-1

我正在创建一个Android应用程序能够从web服务恢复SMS。恢复短信:创建线程,如果不存在

我在现有的会话中插入短信,它工作正常。 但是,如果对话不存在,短信将被恢复,但它们不会出现在短信应用程序中。

我想我必须创建一个新线程(新对话)。

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); 

应用程序崩溃与错误:

MmsSmsProvider不支持删除,插入或更新此 URI.content:// MMS短信/通话简单=真

回答

2

这个例子给你一个threadId,如果接收者不存在,它将创建一个新的ID,否则它将返回现有的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; 
}