Android(安卓)8.1发送广播受限问题
16lz
2021-01-26
之前在android7.0以及之前的版本中,发广播是一件很容易的,一句代码搞定:隐式广播,sendBroadcast(new Intent("android.intent.action.test"));
问题来了,可是在8.1上这样是行不通的。在PhoneWindowManager.java
发按键广播,就是接受不到,抓了log,报错如下:
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.test flg=0x10 (has extras)
翻了相关资料后,明白了。Android O版本对后台进程做了限制广播的发送,对隐式广播也做了限制;必须要指定接受广播类的包名和类名:
Intent intent = new Intent("android.intent.action.test");intent.setComponent(new ComponentName("接受广播的类的包名","接受广播的类的包名+类名")); mContext.sendBroadcast(intent);
更多相关文章
- AndroidStudio3.4+ 导入旧版本moudle出现各种问题,禁用androidx
- Android(安卓)NDK配、编译、调试
- 百度地图长按事件
- linux下编译MTK android的环境搭建
- Android(安卓)API Level 与SDK版本对照表
- Could not find com.android.tools.build:gradle:3.6.2.
- com.android.build.api.transform.TransformException
- qt for android opencv 笔记
- 错误 java.lang.NoSuchMethodError: No static method getFont