从号码中删除所有邮件

时间:2014-06-20 14:30:58

标签: android messages

我想从一个数字中删除所有消息(in,out,draft,....)。

我的代码是:

public void deleteAllMessages(String number) {

    Cursor cursor = context.getContentResolver().query(SMS_URI_ALL,
            new String[] { "_id", "address" }, null, null, null);

    int x = 0;
    while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String address = cursor.getString(1);
        if (address.equals(number)) {
            int delete = context.getContentResolver().delete(SMS_URI_ALL,
                    "_id=" + id, null);
            x++;
        }
    }

    Log.i(getClass().getName(), "For:" + number + " delete MSG: " + x);

}

编辑: 我试试

I try with  `context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);`

但结果相同......

x的计数是对的,但没有删除!为什么? 如何删除号码中的所有邮件? 我使用的Android版本是由Nexus 4提供的4.4.2。

1 个答案:

答案 0 :(得分:1)

我正在使用abortBroadcast()的同一个项目,它没有删除短信但是停止向另一个接收器广播

Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                String date = c.getString(3);
                Log.e("log>>>",
                        "0>" + c.getString(0) + "  1>" + c.getString(1)
                                + "  2>" + c.getString(2) + "  3>"
                                + c.getString(3) + "  4>" + c.getString(4)
                                + "  5>" + c.getString(5));

                if (message.equals(body) && address.equals(number)) {


                    this.abortBroadcast();
                }
            } while (c.moveToNext());
        }