接收后删除sms android没有延迟

前端之家收集整理的这篇文章主要介绍了接收后删除sms android没有延迟前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些代码以编程方式删除 Android SMS消息,但当我尝试在onReceive中删除它时,没有删除短信.

删除短信的示例代码

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);

    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);
            }
        } while (c.moveToNext());
    }
} catch (Exception e) {
    // mLogger.logError("Could not delete SMS from inBox: " +
    // e.getMessage());
}

当我将其粘贴到onReceived时,则不会删除新的SMS.

解决方法

您需要为清单文件添加权限并提高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();
}

就这样

干杯

Ayush Shah

原文链接:https://www.f2er.com/android/315303.html

猜你在找的Android相关文章