7

我有一些代码可以以编程方式删除 Android SMS 消息,但是当我尝试在 onReceive 中删除它时,不会删除任何 SMS。

删除短信的示例代码

try {
    // mLogger.logInfo("Deleting SMS from inbox");
    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(
        uriSms, new String[] { "_id", "thread_id", "address", "person",
        "date", "body" }, null, 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);

            if (message.equals(body) && address.equals(number)) {
                // mLogger.logInfo("Deleting SMS with id: " + threadId);
                context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
            }
        } while (c.moveToNext());
    }
} catch (Exception e) {
    // mLogger.logError("Could not delete SMS from inbox: " +
    // e.getMessage());
}

当我把它粘贴进去时onReceived,新的短信不会被删除。

4

2 回答 2

5

您需要为清单文件添加权限并增加SMS Receiver类的优先级

<receiver android:name=".SMSReceiver" > 
    <intent-filter android:priority="1000"> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver>

这是因为它会在任何操作系统级别的操作(如保存短信、通知、短信声音等)之前调用 SMSReceiver。

然后在 SMSReceiver onReceive()中,您需要添加abortBroadcast()以中止任何进一步的广播

public void onReceive(Context context, Intent intent) {
  abortBroadcast();
}

就这样

干杯

阿尤什·沙阿

于 2013-03-31T21:03:11.783 回答
0

sms您必须删除Smsid

getContentResolver().delete(Uri.parse("content://sms/" + smsid), null, null);
于 2015-05-14T05:03:04.970 回答