android - “导出的接收器不需要许可";在打算从系统服务接收的接收器上 [英] android - "Exported receiver does not require permission" on receivers meant to receive from system services

本文介绍了android - “导出的接收器不需要许可";在打算从系统服务接收的接收器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AndroidManifest 中声明了一些接收器:

I have some receivers declared in my AndroidManifest :

<!-- no warning -->
<receiver
    android:name=".receivers.TriggerMonitoringBootReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<!-- no warning -->
<receiver
    android:name=".receivers.ScanResultsReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.net.wifi.SCAN_RESULTS" />
    </intent-filter>
</receiver>

<!-- warning : Exported receiver does not require permission-->
<receiver
    android:name=".receivers.BatteryMonitoringReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="@string/intent_action_setup_alarm" />
        <action android:name="@string/intent_action_cancel_alarm" />
        <action android:name="@string/intent_action_monitor" />
    </intent-filter>
</receiver>

第一个用于接收 BOOT_COMPLETED 操作.第二个是为了接收android.net.wifi.SCAN_RESULTS.第三个是为了接收我广播的一些动作(intent_action_monitor)和AlarmManager 广播的一些动作(intent_action_setup_alarm 等).

The first one is meant to receive a BOOT_COMPLETED action. The second is meant to receive android.net.wifi.SCAN_RESULTS. The third one is meant to receive some actions I broadcast (intent_action_monitor) and some actions broadcasted by the AlarmManager (intent_action_setup_alarm etc).

两个问题:

  • 为什么我没有在所有接收器上收到警告?
  • 我需要为要从系统服务接收的接收器设置什么权限以纠正警告(我明白这是怎么回事,无论如何我都不希望任何人使用我的接收器)? exported="false" 用于启动接收器、wifi接收器、警报接收器等?
    我想过对 android:protectionLevel="signatureOrSystem" 使用自定义权限,但文档建议不要同时使用 保护级别自定义权限.那么我应该如何处理这个警告?
  • Why don't I get the warning on all receivers?
  • What permissions do I need to set for receivers meant to receive from system services to correct the warning (I understand what it is about and I don't want anyone to use my receivers anyway) ? Will exported="false" do for boot receivers, wifi receivers, alarm receivers etc?
    I thought of using a custom permission with android:protectionLevel="signatureOrSystem" but the docs advise against both this protection level and custom permissions. So how I should handle this warning ?

文档和/或一些代码的链接将不胜感激.

Links to the docs and/or some code will be much appreciated.

推荐答案

为什么我没有在所有接收器上收到警告?

Why don't I get the warning on all receivers ?

因为前两个明确设计为由Android广播.最后一个未知,部分原因是您没有提供字符串资源值,也可能是因为它们是您自己的唯一操作字符串.

Because the first two are clearly designed to be broadcast by Android. The last one is unknown, partly because you did not supply the string resource values, and possibly because they are your own unique action strings.

我需要为打算从系统服务接收的接收器设置什么权限以纠正警告

What permissions do I need to set for receivers meant to receive from system services to correct the warning

正确的解决方法是删除.如果您正在广播这些 Intent,或者如果您将 Intent 包装在 getBroadcast() PendingIntent 中,您不需要动作字符串.使用将 Java 类对象作为第二个参数的 Intent 构造函数,并使用它:

The correct solution is to delete the <intent-filter>. If you are broadcasting these Intents, or if you are wrapping an Intent in a getBroadcast() PendingIntent, you do not need action strings. Use the Intent constructor that takes the Java class object as the second parameter, and use that:

new Intent(this, BatteryMonitoringReceiver.class)

如果需要,您仍然可以将操作字符串附加到该 Intent,但您可以转储 <intent-filter>(路由将基于在提供的组件上,在本例中为 Java 类).

You are welcome to still attach an action string to that Intent, if you want, but you can dump the <intent-filter> (routing will be based on the supplied component, in this case the Java class).

仅当您希望操作系统或第三方应用自行启动 Intent(执行 PendingIntent<您创建的/code> 不算在内).

Only use an <intent-filter> when you are expecting the OS or third-party apps to initiate the Intent themselves (executing a PendingIntent that you created does not count).

这篇关于android - “导出的接收器不需要许可";在打算从系统服务接收的接收器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆