博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 8.0 的广播无法接收 Background execution not allowed-PACKAGE_ADDED
阅读量:4147 次
发布时间:2019-05-25

本文共 4713 字,大约阅读时间需要 15 分钟。

0. 前言

Android 8.0 的广播无法接收 Background execution not allowed-PACKAGE_ADDED,该现象已经确认。

基于对广播的严格控制,大幅度减少了静态广播给第三方应用的使用,也是出于限制后台启动的作用。

故类似 android.intent.action.PACKAGE_ADDED 已经无法通过静态广播来监听,但是我们可以通过动态广播注册监听。当然动态广播要求应用要活着,如果被kill掉了,仍旧无法接受到。

1. 现象-广播无法接受日志

BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.android.cts.launcherapps.simpleapp flg=0x4000010 (has extras) } to com.android.lava.powersave/.recevier.LavaPowerSaveRecevier

2. 查看CTS的代码实现

2.1 CTS 应用可以正常接受

06-04 14:18:56.149978  6458  8036 I ChromeSync: [Sync,SyncIntentOperation] Handling the intent: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.android.cts.launcherapps.simpleapp flg=0x4000010 cmp=com.google.android.gms/.chimera.GmsIntentOperationService (has extras) }.

2.2 查看 CTS 源码位置 1

grep -irn “com.android.cts.launcherapps.simpleapp” cts/

root@69959bbb90c6:/home/suhuazhi/8.1/liangxiang# cts/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java:45:    static final String SIMPLE_PACKAGE_NAME = "com.android.cts.launcherapps.simpleapp";cts/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java:51:            "com.android.cts.launcherapps.simpleapp.SimpleActivityStartService.RESULT";cts/tests/app/src/android/app/cts/InstrumentationTest.java:61:    static final String SIMPLE_PACKAGE_NAME = "com.android.cts.launcherapps.simpleapp";cts/tests/app/src/android/app/cts/ActivityManagerTest.java:52:    static final String SIMPLE_PACKAGE_NAME = "com.android.cts.launcherapps.simpleapp";cts/tests/app/AndroidManifest.xml:37:            android:targetPackage="com.android.cts.launcherapps.simpleapp">cts/tests/app/AndroidManifest.xml:41:            android:targetPackage="com.android.cts.launcherapps.simpleapp"cts/tests/app/AndroidManifest.xml:42:            android:targetProcesses="com.android.cts.launcherapps.simpleapp:other">cts/tests/app/AndroidManifest.xml:46:                     android:targetPackage="com.android.cts.launcherapps.simpleapp"cts/tests/app/AndroidManifest.xml:51:                     android:targetPackage="com.android.cts.launcherapps.simpleapp"cts/tests/app/AndroidManifest.xml:52:                     android:targetProcesses="com.android.cts.launcherapps.simpleapp:other,com.android.cts.launcherapps.simpleapp">

上述发现没有进行静态广播注册了

2.3 查看 CTS 源码位置 2

  • grep -irn “android.intent.action.PACKAGE_ADDED” cts/

  • grep -irn “Intent.ACTION_PACKAGE_ADDED” cts/

root@69959bbb90c6:/home/suhuazhi/8.1/liangxiang# grep -irn "android.intent.action.PACKAGE_ADDED" cts/cts/tests/tests/permission2/res/raw/android_manifest.xml:39:    
cts/tests/tests/permission2/src/android/permission2/cts/ProtectedBroadcastsTest.java:34: Intent.ACTION_PACKAGE_ADDED,cts/apps/CtsVerifier/src/com/android/cts/verifier/admin/DeviceAdminUninstallTestActivity.java:97: final IntentFilter packageAddedFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);cts/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationHiddenTest.java:43: PACKAGE_INTENT_FILTER.addAction(Intent.ACTION_PACKAGE_ADDED);cts/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationHiddenTest.java:99: if (Intent.ACTION_PACKAGE_ADDE .equals(intent.getAction())) {

感觉很像动态广播监听数据

2.4 查看动态广播实现

cts/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationHiddenTest.java:43

package com.android.cts.deviceandprofileowner;public class ApplicationHiddenTest extends BaseDeviceAdminTest {
static { PACKAGE_INTENT_FILTER = new IntentFilter(); PACKAGE_INTENT_FILTER.addAction(Intent.ACTION_PACKAGE_ADDED); PACKAGE_INTENT_FILTER.addAction(Intent.ACTION_PACKAGE_REMOVED); PACKAGE_INTENT_FILTER.addDataScheme("package"); } private class ApplicationHiddenReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { Uri uri = intent.getData(); if (uri == null) { return; } String pkgName = uri.getSchemeSpecificPart(); if (!PACKAGE_TO_HIDE.equals(pkgName)) { return; } if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { Log.d(TAG, "Received PACKAGE_ADDED broadcast"); mAddedSemaphore.release(); } else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { Log.d(TAG, "Received PACKAGE_REMOVED broadcast"); mRemovedSemaphore.release(); } } }

2.5 结论

Android 8.0 应用安装和卸载的广播目前 cts 主要使用动态广播监听事件

转载地址:http://bgcti.baihongyu.com/

你可能感兴趣的文章
获取SpringCloud gateway响应的response的并进行修改
查看>>
SpringCloud项目整合OSS对象存储
查看>>
Java重写equals方法
查看>>
Nginx+Windows搭建域名访问环境
查看>>
压力测试和性能优化实践
查看>>
缓存及分布式锁
查看>>
异步与线程池及异步编排
查看>>
消息队列RabbitMQ
查看>>
git进行项目版本管理
查看>>
SpringSecurity 退出登录使JWT失效的解决方案
查看>>
接口幂等性问题处理
查看>>
本地事务与分布式事务
查看>>
RabbitMQ消息队列处理库存解锁及关闭订单问题
查看>>
netty学习笔记
查看>>
mvcc多版本并发控制
查看>>
HashMap实现原理
查看>>
ThreadLocal技术详解
查看>>
ConcurrentHashMap解析
查看>>
TCP协议三次握手、四次挥手以及TCP窗口滑动机制
查看>>
【排序算法】- 希尔排序
查看>>