我有一个包含 6 个应用程序的项目。当一个应用程序收到通知时,其他五个应用程序也会通过隐式广播接收器收到通知。
但在 android Oreo 中,隐式广播接收器已被弃用。所以我不能一起通知其他5个应用程序。
无论如何,我可以在 android oreo 中执行相同的功能吗?
最佳答案
引用我自己的话,来自the blog post链接至 a comment :
If you are sending implicit broadcasts, you can break through the ban by finding the receivers and sending individual explicit broadcasts instead:
private static void sendImplicitBroadcast(Context ctxt, Intent i) {
PackageManager pm=ctxt.getPackageManager();
List<ResolveInfo> matches=pm.queryBroadcastReceivers(i, 0);
for (ResolveInfo resolveInfo : matches) {
Intent explicit=new Intent(i);
ComponentName cn=
new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name);
explicit.setComponent(cn);
ctxt.sendBroadcast(explicit);
}
}
Unfortunately, this brings back the process churn, and if lots of developers do this, there may be reprisals from Google. You might try introducing some delay between the broadcasts, inside the loop, to spread out the impact. However, this starts to get tricky if you spread it out over more than a few seconds (e.g., do you now need an
IntentService
and aWakeLock
? what if your process is terminated before the broadcast loop is completed?).
关于android - android Oreo 中的隐式广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085518/