删除传入的短信

时间:2011-10-31 12:54:45

标签: android sms

    @Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;

    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            if (msgs[i].getOriginatingAddress().equals(
                    SmsModuleActivity.number))
                deleteMessage(context, msgs[i]);
        }
    }
}

private void deleteMessage(Context context, SmsMessage msg) {
    Uri deleteUri = Uri.parse("content://sms/inbox/");
    Cursor c = context.getContentResolver().query(deleteUri, null, null,
            null, null);
    while (c.moveToNext()) {
        try {
            String pid = c.getString(0);
            String uri = "content://sms/inbox/" + pid;
            ContentValues values = new ContentValues();
            values.put("read", true);
            context.getContentResolver().update(Uri.parse(uri), values,
                    null, null);
            context.getContentResolver().delete(Uri.parse(uri), null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

未删除新传入的短信。当我从这个号码获取短信时,它只删除此发送者的旧短信,但没有新消息。我需要删除特定号码的新传入消息(参见源代码。)

3 个答案:

答案 0 :(得分:3)

您可以使用abortBroadcast()代替deleteMessage(context, msgs[i])来阻止传入的短信到达收件箱,然后您就不需要将其删除。

答案 1 :(得分:2)

试试这段代码:

int id = cursor.getInt(cursor.getColumnIndex("_id"));
getContentResolver().delete(Uri.parse("content://sms/"), "_id="+id, null);

答案 2 :(得分:0)

在第一个活动课程中收到短信后,使用此代码删除最后的短信。别忘了 在主节点文件中原谅短信读写权限。

  Uri uriSms = Uri.parse("content://sms/");
            Cursor c = getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address","person", "date", "body" }, null, null, null);
            if (c != null && c.moveToFirst()) {

                    long id = c.getLong(0);
                    getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);

            }`enter code here`