[英]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>
...
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.