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