中止/取消广播

发布于 2024-10-20 09:45:51 字数 463 浏览 2 评论 0原文

我想要的:我想成为第一个接收短信广播的人,并且如果短信仅对我感兴趣,我想取消广播,以便广播不会到达任何其他应用程序/接收器(默认消息应用程序等) 。 我所知道的是:

  • SmsDisptacher.java 使用可以取消/中止的orderedBroadcasts。

我不知道的是:

  • 是否可以为其他应用程序/接收器(即除了您自己之外)取消订购广播。

我已经尝试过第一个接收广播:

  • intent-filter android:priority="1000"

我已经尝试过取消广播:

  • AbortBroadcast();
  • BroadcastReceiver.setResultCode(RESULT_CANCELED)
  • clearAbortBroadcast()

What I want: I want to be the first to receive the Sms Broadcast and I want to cancel the broadcast if SMS is of my interest only, so that The broadcast doesn't reach any other app/receiver (Default messaging app etc.).
What I know is:

  • SmsDisptacher.java uses orderedBroadcasts that can be can canceled/aborted.

What I don't know is:

  • If orderedBrodcasts can be canceled for other apps/receivers i.e other than yourself.

what I have tried for being the first to receive the Broadcast:

  • intent-filter android:priority="1000"

What i have tried for canceling broadcast already:

  • AbortBroadcast();
  • broadcastReceiver.setResultCode(RESULT_CANCELED)
  • clearAbortBroadcast()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-10-27 09:45:51

这当然是可能的。
在清单中注册您的接收器,如下所示:

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

在接收器的 onReceive 方法中写入:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(SMS_RECEIVED)) {
        //do your stuff
        abortBroadcast();
    }
}

请记住在清单中设置接收短信的权限,如下所示:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

如果没有正确的权限集,它将默默失败并显示以下 logcat 消息:

07-13 12:54:13.208: WARN/ActivityManager(201): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to com.mypackage requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

最后,它是可能另一个应用程序已经注册了比您的优先级更高的接收器,因此它会在您取消广播之前接收广播。因此,如果所有其他方法都失败,请尝试尝试更高的优先级值。

It is certainly possible.
Register your receiver in your manifest like this:

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

In your receiver's onReceive method write:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(SMS_RECEIVED)) {
        //do your stuff
        abortBroadcast();
    }
}

Remember to set the permission to receive sms messages in the manifest like this:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

Without the correct permission set it will fail silently with the following logcat message:

07-13 12:54:13.208: WARN/ActivityManager(201): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to com.mypackage requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

Finally, it is possible that another application has registered a receiver with an even higher priority than yours, and that it therefore receives the broadcast before you cancel it. So if all else fails, try experimenting with higher priority values.

×眷恋的温暖 2024-10-27 09:45:51

这是可能的,我做了一个教程,请检查此处此处
只需中止层次结构顶部的广播

it's possible to do that, i did a tutorial check it please here and here
just abort the broadcast on the top of the hierarchy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文