android abortBroadcast 不会阻止短信的广播
在我的 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 不起作用?
in my onReceive method I have this code:
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();
}
where number = "29853" - number messages from which I wanna catch and not save in Inbox.
This code works correctly - if sms is from number the first Toast works and it prints the content of the message, if sms is not from number "Not from needed number" is printed. The problem is that abortBroadcast doesn't make its businness - the message from number is still in the Inbox of the phone although receiver's priority is 1000:
<receiver android:name=".service_classes.MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"
android:priority="1000" />
</intent-filter>
</receiver>
What's the problem - why doesn't abortBroadcast work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
android:priority
移动到它所属的意图过滤器。那么您更有可能拥有优先权,并且您的取消将会起作用。
我已经测试过它并且确实有效。
move the
android:priority
to the intent-filter where it belongs.then you will more likely have priority and your canceling will work.
I have tested it and it does work.
从 Android 4.4 开始,您无法中止这些广播。以下是显示此内容的 AOSP 源链接:
https://github.com/CyanogenMod/android_frameworks_opt_telephony/blob/d43b5b1ca91e0aac0c697546a5cb341ffa758e0b/src/java/com/android/internal/telephony/ImsSMSDispatcher.java#L605
From Android 4.4 you cannot abort these broadcasts. Here's a link to the AOSP sources that show this:
https://github.com/CyanogenMod/android_frameworks_opt_telephony/blob/d43b5b1ca91e0aac0c697546a5cb341ffa758e0b/src/java/com/android/internal/telephony/ImsSMSDispatcher.java#L605
abortBroadcast()
不会从收件箱中删除消息,它只是抑制状态栏通知。要从收件箱中删除短信,请参阅 这个链接The
abortBroadcast()
does not DELETE messages from the inbox, it is just suppressing the status bar notification. To delete the sms from the inbox, refer to this link我不久前用过这个,它很有效,我可以拒绝一些消息的显示,但现在不起作用。我认为 android 不再让用户完全控制 SMS_RECEIVED 事件。
I've used this a while ago and it was working, I could reject some messages to show-up but this doesn't work now. I think android no longer lets user take all control over SMS_RECEIVED event.