android abortBroadcast不会阻止短信被广播

Ser*_*gey 6 android broadcastreceiver

在我的onReceive方法中我有这个代码:

if (from.equals(number)) {
    abortBroadcast();
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);
    Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(context, "Not from needed number", Toast.LENGTH_SHORT).show();
}   
Run Code Online (Sandbox Code Playgroud)

其中number ="29853" - 我希望捕获的数字消息,而不是保存在收件箱中.

此代码正常工作 - 如果sms来自第一个Toast的数字,它打印消息的内容,如果sms不是来自数字"Not from needed number"打印.问题是abortBroadcast没有使其业务 - 虽然接收者的优先级是1000,但来自号码的消息仍然在手机的收件箱中:

<receiver android:name=".service_classes.MyReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"
                android:priority="1000" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

有什么问题 - 为什么不abortBroadcast工作?

小智 8

移动android:priority到它所属的intent-filter.

 <intent-filter android:priority="9999999"> 
Run Code Online (Sandbox Code Playgroud)

那么你将更有可能拥有优先权,你的取消将有效.我测试了它,它确实有效.

  • 根据文档:`值必须是整数,例如"100".数字越大,优先级越高.默认值为0.该值必须大于-1000且小于1000.因此,最大数字应为999. http://developer.android.com/guide/topics/manifest/intent-filter-element. HTML (5认同)

and*_*oob 3

abortBroadcast()不会从收件箱中删除邮件,它只是抑制状态栏通知。要从收件箱中删除短信,请参阅此链接

  • 我认为可以,但在 4.4+ 版本上不行。 (2认同)