Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx

这篇具有很好参考价值的文章主要介绍了Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

带android:sharedUserId=“android.uid.system” 发送广播时,会出现 Sending non-protected broadcast 异常提醒;

原因:
Ams在发送广播时,对于systemApp(系统应用),会要求发送广播必须是声明在frameworks\base\core\res\AndroidManifest.xml里面的protected-broadcast。这是为了提醒 系统应用开发者要将 broadcast 添加到protected-broadcast,因为非 protexted-broadcast 广播是可以被三方应用发送的。定义为 proected-broadcast 可以避免三方垃圾应用也发送这些广播来捣蛋。

查找问题

首先去搜索问题出现的位置 ,去AndroidXRef网站搜索下

定位到frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

类中,在checkBroadcastFromSystem中抛出该异常。

checkBroadcastFromSytem是用于检测系统应用发出的广播是否安全,如果发出的广播不安全,就会抛出no-protected broadcast异常,提醒系统应用开发者。

为什么要有这个机制?

这是为了提醒 系统应用开发者去将 broadcast添加为protected-broadcast,因为非 protected-broadcast 广播是可以被三方应用发送的。 而定义为 proected-broadcast 就能防止恶意的三方应用模仿系统应用去发出该广播。

private void checkBroadcastFromSystem(Intent intent, ProcessRecord callerApp,
            String callerPackage, int callingUid, boolean isProtectedBroadcast, List receivers) {
        if ((intent.getFlags() & Intent.FLAG_RECEIVER_FROM_SHELL) != 0) {
            // Don't yell about broadcasts sent via shell
            return;
        }
 
        final String action = intent.getAction();
        if (isProtectedBroadcast
                || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
                || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MEDIA_BUTTON.equals(action)
                || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
                || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MASTER_CLEAR.equals(action)
                || Intent.ACTION_FACTORY_RESET.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
                || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
                || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
                || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)
                || AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION.equals(action)
                || AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION.equals(action)) {
            // Broadcast is either protected, or it's a public action that
            // we've relaxed, so it's fine for system internals to send.
            return;
        }
 
        // This broadcast may be a problem...  but there are often system components that
        // want to send an internal broadcast to themselves, which is annoying to have to
        // explicitly list each action as a protected broadcast, so we will check for that
        // one safe case and allow it: an explicit broadcast, only being received by something
        // that has protected itself.
        if (intent.getPackage() != null || intent.getComponent() != null) {
            if (receivers == null || receivers.size() == 0) {
                // Intent is explicit and there's no receivers.
                // This happens, e.g. , when a system component sends a broadcast to
                // its own runtime receiver, and there's no manifest receivers for it,
                // because this method is called twice for each broadcast,
                // for runtime receivers and manifest receivers and the later check would find
                // no receivers.
                return;
            }
            boolean allProtected = true;
            for (int i = receivers.size()-1; i >= 0; i--) {
                Object target = receivers.get(i);
                if (target instanceof ResolveInfo) {
                    ResolveInfo ri = (ResolveInfo)target;
                    if (ri.activityInfo.exported && ri.activityInfo.permission == null) {
                        allProtected = false;
                        break;
                    }
                } else {
                    BroadcastFilter bf = (BroadcastFilter)target;
                    if (bf.requiredPermission == null) {
                        allProtected = false;
                        break;
                    }
                }
            }
            if (allProtected) {
                // All safe!
                return;
            }
        }
 
        // The vast majority of broadcasts sent from system internals
        // should be protected to avoid security holes, so yell loudly
        // to ensure we examine these cases.
        if (callerApp != null) {
            Log.wtf(TAG, "Sending non-protected broadcast " + action
                            + " from system " + callerApp.toShortString() + " pkg " + callerPackage,
                    new Throwable());
        } else {
            Log.wtf(TAG, "Sending non-protected broadcast " + action
                            + " from system uid " + UserHandle.formatUid(callingUid)
                            + " pkg " + callerPackage,
                    new Throwable());
        }
    }

如上代码, 对于广播,checkBroadcastFromSystem方法有三种检测。

  1. 如果Action 是Shell 发出的,则直接返回,不发出警告

  2. 如果Action 是protected-broadcast广播或一些指定的Action,不发出警告

  3. 如果Action 是指定了包名或者Compononent信息,只有特定应用能接收,则当所有receiver 均根据receiverPermisson 对权限进行了顾虑,则不发出警告

如果系统应用不满足以上三种情况,则发送公共广播时会被警告

如何解决 non-protected broadcast警告

从系统源码层面

1.在checkBroadcastFromSystem 处修改

 if (isProtectedBroadcast
                || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
                || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MEDIA_BUTTON.equals(action)
                || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
                || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
                || Intent.ACTION_MASTER_CLEAR.equals(action)
                || Intent.ACTION_FACTORY_RESET.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
                || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
                || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
                || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
                || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)
                || AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION.equals(action)
                || AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION.equals(action)) {
            // Broadcast is either protected, or it's a public action that
            // we've relaxed, so it's fine for system internals to send.
            return;
        }
复制代码

2.在/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java#isProtectedBroadcast方法处修改

Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx

3.在/frameworks/base/core/res/AndroidManifest.xml 处增加 protected-broadcast 标签

Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx

在应用层面修改

  1. 在系统应用的 AndroidManifest.xml 处增加 protected-broadcast 标签,且apk放在 /system/priv-app 目录下

  2. 指定接收者.获取所有接收该广播的ResolveInfo,发送广播。文章来源地址https://www.toymoban.com/news/detail-407567.html

到了这里,关于Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • stable diffusion 运行时报错: returned non-zero exit status 1.

    运行sh run.sh安装stable diffusion时报错:ImportError: cannot import name \\\'builder\\\' from \\\'google.protobuf.internal\\\' (stable-diffusion-webui/venv/lib/python3.8/site-packages/google/protobuf/internal/__init__.py) 原因:python版本过低,使用3.10 ubuntu安装python3.10: 重新安装venv: python3.10 -m venv venv 报错:Error: Command \\\'[\\\'stable

    2024年02月12日
    浏览(33)
  • 安卓广播发送接收流程

    本文基于Andorid 11。 动态注册广播接收器,参数:BroadcastReceiver, IntentFilter。 静态注册广播,安卓8以后除几个特殊的广播外,静态注册方式只能接收显示广播。 查看动态注册广播流程: 1.2 ContextImpl.registerReceiverInternal 传入IIntentReceiver对象作为参数,调用AMS.registerReceiverWithFea

    2024年04月17日
    浏览(39)
  • sendBroadcast发送广播

    这几天学习Receiver的时候,发现android 8.0之后对隐式注册的广播接收器做出了限制,除了一些例外情况,我试了几种情况总结下来,以防忘记. 使用sendBroadcast(intent)时 如使用Intent的构造方法直接传递Context和Class或使用intent.setComponent或者setClass或setClassName 这种情况下, 查看源代码可知

    2023年04月08日
    浏览(26)
  • TCP/IP UDP广播无法发送或接收

    在看《TCP/IP 网络编程》这本书的时候,看到广播那一节,跟着书上写代码,怎么写都不行,广播就是没法发送/接收,发送端一直在发送数据,接收端就是没有反应。 对了好几遍源码,没有问题。实在是愁人。 最后查了很多资料,确定是网卡的问题。 现在的计算机都是多网

    2024年02月04日
    浏览(35)
  • QT网络编程之实现UDP广播发送和接收

    一.UDP广播介绍 UDP广播地址固定IP地址为:XXX.XXX.XXX.255。 如果向全网段发送广播消息,那么广播地址为:255.255.255.255; 如果向单个网段发送广播消息,例如你的IP是192.168.31.104,那么广播地址为192.168.31.255。 广播消息接收方需要绑定0.0.0.0地址并监听指定端口即可收到广播的群

    2024年03月25日
    浏览(45)
  • C/C++ Socket UDP 广播消息的发送与接收

    局域网内全网段广播消息的IP地址为:255.255.255.255,向该IP地址发送广播消息,局域网下的任何网段的客户机都能收到广播。 对于发送端,如果你只想给某个特定的网段发送消息,例如你的IP地址为192.168.31.107,那么你的广播地址是192.168.31.255,向该广播地址发送广播消息,只

    2024年02月12日
    浏览(30)
  • Adb发送特定广播给App和App获取权限的命令

    最近在做Autostart,但是没有bench R1环境 目前在模拟器上调试,需要调试自定义的广播和获取悬浮窗权限(因为这个app需要在开机未启动app的情况启动服务区获取传感器信号然后全局弹窗)。 需要先adb root adb remount 1.发送广播给指定应用: 2.包名获取悬浮窗(或者其他权限)

    2024年02月13日
    浏览(28)
  • adb shell模拟发送安卓广播的入门知识和100个实例讲解

    adb shell模拟发送安卓广播的入门知识和实例讲解: 广播是一种Android系统提供的一种机制,用于在系统中传递事件或消息。广播可以是系统级别的,也可以是应用级别的。系统级别的广播可以被所有应用接收,应用级别的广播只能被同一应用中的组件接收。 广播分为两种类型

    2024年02月09日
    浏览(37)
  • DHCP协议详解,报文内容,如何查看报文,为什么offer报文会以广播的形式进行发送

    dhcp地址分配协议,目前有dhcpv4以及dhcpv6,分别作用于ipv4与ipv6的网络中。 主要作用:dhcp服务端通过dhcp协议下发ip地址给到客户端(pc,终端),使得pc能有上网的能力。 1.dhcp交互图 2.dhcp报文交互过程中,有大部分的报文都是广播报文。 客户端拥有ip前,是可以接收所有的广

    2024年02月06日
    浏览(23)
  • Android 蓝牙广播

    ( BluetoothAdapter.ACTION_STATE_CHANGED ):当蓝牙状态发生变化时发送,可以用于检测蓝牙的开启或关闭状态。 提供了以下 Extra 值来描述相关信息: BluetoothAdapter.EXTRA_STATE: 表示蓝牙的当前状态。你可以使用 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) 来获取该值。其可

    2024年02月16日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包