删除短信对话

时间:2014-07-21 17:11:41

标签: android database cursor sms android-4.4-kitkat

尝试使用数字删除整个对话,如下所示:

 mContext.getContentResolver().delete(Uri.parse("content://sms/"),"address="+Number, null);

但没有发生,没有力量关闭&它没有删除,任何帮助?,

由于

1 个答案:

答案 0 :(得分:2)

您需要分两步完成。像这样的东西:

A。按地址查询短信息数据库以获取短信的短信ID:

 String numberFilter = "address='"+ phoneNumber + "'";
 String messageid = null;
 Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://sms/"), 
              null, numberFilter, null, null);

 if (cursor.moveToFirst()) {
      messageid = cursor.getString(0);
 }

B。使用消息ID

删除短信
getContentResolver().delete(Uri.parse("content://sms/" + messageid), null, null);

现在,进行多次删除。您报告多次删除失败 使用游标迭代(while cursor.moveToFirst())。我从未尝试过,但我认为

它与删除后无效的游标有关。尝试在每次删除之前重新启动光标,如下所示:

 for (;;) {
     // restart cursor before each delete
     String messageid = null;
     Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://sms/"), 
                  null, numberFilter, null, null);

     if (!cursor.moveToFirst()) {
       break; // nothing more to delete
     }

     // delete single record
     messageid = cursor.getString(0);
     getContentResolver().delete(Uri.parse("content://sms/" + messageid), null, null);
 }

您需要写短信权限:

<uses-permission android:name="android.permission.WRITE_SMS"/>