简体   繁体   English

清单声明的广播接收器不会在 Android 8 上接收自定义广播

[英]Manifest-declared broadcast receiver does not receive custom broadcast on Android 8

First, I created a BroadcastReceiver with empty body.首先,我创建了一个空体的BroadcastReceiver Then, I added it to the AndroidManifest.xml.然后,我将其添加到 AndroidManifest.xml 中。 But I found out that BroadcastReceiver declared in manifest does not receive any broadcast.但是我发现清单中声明的BroadcastReceiver没有收到任何广播。 I sent broadcast by我发送广播

sendOrderedBroadcast(new Intent("com.example.action"), null)

or要么

adb shell am broadcast -a com.example.action

Both methods works on Android 7 but it does not work on Android 8. However, if the BroadcastReceiver is declared through registerReceiver , then it can still receive the broadcast.这两种方法都适用于 Android 7,但不适用于 Android 8。但是,如果BroadcastReceiver是通过registerReceiver声明的,那么它仍然可以接收广播。

On the other hand, android.hardware.usb.action.USB_DEVICE_ATTACHED works fine on both Android 7 and 8.另一方面, android.hardware.usb.action.USB_DEVICE_ATTACHED在 Android 7 和 8 上都可以正常工作。

I want to ask why does it happen?我想问一下为什么会这样? I have tested it in both emulator and a physical device.我已经在模拟器和物理设备中对其进行了测试。 They have the same behavior.他们有相同的行为。

AndroidManifest.xml AndroidManifest.xml

...
<receiver
    android:name=".device.UsbBroadcastReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        <action android:name="com.example.action" />
    </intent-filter>
    <meta-data
        android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />
</receiver>
...

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.作为 Android 8.0(API 级别 26)后台执行限制的一部分,面向 API 级别 26 或更高级别的应用无法再在其清单中为隐式广播注册广播接收器。

Read this 读这个

Manifest Based broadcast working for me in android 8基于清单的广播在 android 8 中为我工作

Broadcast restriction by android is not applicable for all the broadcasts some can still be registered inside manifest . android 的广播限制不适用于所有广播,有些仍然可以在 manifest 中注册。 But do check latest documentation since these may way depending on android version .但是请检查最新的文档,因为这些可能取决于 android 版本。

 <receiver
            android:name=".UsbReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM