Background execution not allowed

阅读更多
11-05 10:08:18.058   673   736 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.TEST flg=0x400010 } to mvp.com.presentationapp/.PresentationScreenReceiv
er

在Android O上处于安全的考虑,google不允许广播隐试intent了。可以查看源码 http://androidxref.com/8.1.0_r33/xref/frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java#1275

267                    } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
1268                            || (r.intent.getComponent() == null
1269                                && r.intent.getPackage() == null
1270                                && ((r.intent.getFlags()
1271                                        & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
1272                                && !isSignaturePerm(r.requiredPermissions))) {
1273                        mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
1274                                component.getPackageName());
1275                        Slog.w(TAG, "Background execution not allowed: receiving "
1276                                + r.intent + " to "
1277                                + component.flattenToShortString());
1278                        skip = true;
1279                    }
1280                }
1281            }


我们可以为Intent加上FLAG_RECEIVER_INCLUDE_BACKGROUND(0x01000000) 这个标志位

通过adb shell am broadcast -a android.intent.action.TEST -f 0x01000000来验证:
11-05 10:08:28.042  1480  1480 E PresentationScreenReceiver: onReceive: android.intent.action.TEST false

你可能感兴趣的:(android)