【问题标题】:android abortBroadcast doesn't stop sms from being broadcastedandroid abortBroadcast 不会阻止短信被广播
【发布时间】:2011-12-05 13:33:08
【问题描述】:

在我的 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();
}   

其中 number = "29853" - 我想从中捕获但不保存在收件箱中的邮件编号。

此代码正常工作 - 如果短信来自号码,则第一个 Toast 工作并打印消息的内容,如果短信不是来自号码,则打印“不是来自需要的号码”。问题是 abortBroadcast 没有成功 - 尽管接收者的优先级为 1000,但来自号码的消息仍在手机的收件箱中:

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

有什么问题 - 为什么 abortBroadcast 不起作用?

【问题讨论】:

标签: android broadcastreceiver


【解决方案1】:

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

 <intent-filter android:priority="9999999"> 

那么您将更有可能获得优先权,并且您的取消将起作用。 我已经测试过了,它确实有效。

【讨论】:

  • 优先级记录的最大值是多少?你是通过反复试验计算出这个值吗?
  • 记录的数字是1000。但是每个人都使用的数字要高得多,所以基本上目前的情况是“先安装先赢”。
  • 根据文档:The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000. 所以最大数量正好是 999。developer.android.com/guide/topics/manifest/…
【解决方案2】:

【讨论】:

  • 某些功能太危险了,任何应用都无法控制它们,所以这是正确的行为。
【解决方案3】:

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

【讨论】:

  • 我认为可以,但不是在 4.4+ 版本上。
【解决方案4】:

我前段时间用过它,它工作正常,我可以拒绝一些消息显示,但现在不行。我认为 android 不再让用户完全控制 SMS_RECEIVED 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多