如何删除android中特定联系人号码的所有短信

时间:2012-05-19 11:40:57

标签: java android

我有以下代码删除所有短信对话

Uri inboxUri = Uri.parse("content://sms/conversations");
int count = 0;
Cursor c = context.getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
    try {
        // Delete the SMS
        String pid = c.getString(0); // Get id;
        String uri = "content://sms/" + pid;
        count = context.getContentResolver().delete(Uri.parse(uri),
                null, null);
    } catch (Exception e) {
    }
}
return count;

但我想删除特定联系电话的短信,怎么办?

1 个答案:

答案 0 :(得分:2)

您可以使用

删除特定号码的所有短信
private void removeMessage(Context context, String fromAddress) {

    Uri uriSMS = Uri.parse("content://sms/inbox");
    Cursor cursor = context.getContentResolver().query(uriSMS, null, 
                                                               null, null, null);
    cursor.moveToFirst();
      if(cursor.getCount() > 0){
        int ThreadId = cursor.getInt(1);
        Log.d("Thread Id", ThreadId+" id - "+cursor.getInt(0));
        Log.d("contact number", cursor.getString(2));
        Log.d("column name", cursor.getColumnName(2));

        context.getContentResolver().delete(Uri.
                parse("content://sms/conversations/"+ThreadId), "address=?", 
                                                      new String[]{fromAddress});
        Log.d("Message Thread Deleted", fromAddress);
      }
      cursor.close();
}