【发布时间】:2019-07-13 14:25:58
【问题描述】:
在 Android 中使用 implicit 和 explicit 两种类型的广播 - 对于隐式我们将在 AndroidManifest.xml 中声明广播,并且当某些应用发送带有动作的广播时,所有声明广播的应用在他们的清单中,该操作将被调用并完成工作。
由于 Android 从 O 强加了 background execution limits,我不能只发送包含 action 的意图的广播。我必须明确指定包名和接收类名。
现在通过这样做,我能够克服隐式广播限制
String action = "com.android.intent.CUSTOM";
Intent intent = new Intent();
intent.setAction(intent);
//Though this is a deprecated method
List<ResolveInfo> resolvedBroadcasts = List<ResolveInfo> queryBroadcastReceivers(intent, 0, current_user_id);
for (ResolveInfo info : resolvedBroadcasts) {
ServiceInfo serviceInfo = info.serviceInfo;
//Note: Now this is becoming explicit broadcast
intent.setAction(serviceInfo.packageName, serviceInfo.name);
context.sendBroadcast(intent);
}
我在这里遗漏了什么吗?在这一点上感到困惑,如果我能够这样做,那么为什么 Android 会给我施加这个后台执行限制?
【问题讨论】:
标签: android android-8.0-oreo android-broadcast