1、隐式广播权限
android O系统后,隐式广播收到的限制,一定要指定包名才能发送出来。
否则会报错:
Background execution not allowed: receiving Intent { act=android.bluetooth.anw.action.PAIR_REQUEST flg=0x10 (has extras) } to com.anwsdk.sample/.BTMsgReceiver
但是在实际开发测试时,有些旧的app应用还是发送的隐式广播,导致接收不到,临时修改系统源码解决:
源码:\frameworks\base\services\core\java\com\android\server\am\BroadcastQueue.java
processNextBroadcastLocked()对广播进行了判断和处理:
if (!skip) {
final int allowed = mService.getAppStartModeLocked(
info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false, false);
if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
// We won't allow this receiver to be launched if the app has been
// completely disabled from launches, or it was not explicitly sent
// to it and the app is in a state that should not receive it
// (depending on how getAppStartModeLocked has determined that).
if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
Slog.w(TAG, "Background execution disabled: receiving "
+ r.intent + " to "
+ component.flattenToShortString());
skip = true;
} else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
|| (r.intent.getComponent() == null
&& r.intent.getPackage() == null
&& ((r.intent.getFlags()
& Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
&& !isSignaturePerm(r.requiredPermissions))) {
mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
component.getPackageName());
Slog.w(TAG, "Background execution not allowed: receiving "
+ r.intent + " to "
+ component.flattenToShortString());
//取消skip=true,让广播依旧可以发送出去
//skip = true;
}
}
}
2、受保护的广播
报错:
ActivityManager: Sending non-protected broadcast …Throwable at
系统中发送的广播必须是受保护的广播,否则发送失败。
源码:\frameworks\base\core\res\AndroidManifest.xml
增加:
<protected-broadcast android:name="android.intent.action.DOCK_IDLE" />
<protected-broadcast android:name="android.intent.action.DOCK_ACTIVE" />