Android 8.0 的广播无法接收 Background execution not allowed-PACKAGE_ADDED

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:    <protected-broadcast android:name="android.intent.action.PACKAGE_ADDED" />


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 主要使用动态广播监听事件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值