android - android Oreo 中的隐式广播

标签 android broadcastreceiver android-8.0-oreo

我有一个包含 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 a WakeLock? what if your process is terminated before the broadcast loop is completed?).

关于android - android Oreo 中的隐式广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085518/

相关文章:

android - 前台服务在 Android O 的打瞌睡模式下不接收位置更新

android - 矢量图像在 Android 中显示错误

android - 广播接收器未收到 ACTION_BOOT_COMPLETED

android - 是否可以在 Android Studio 中删除我代码中的所有注释?

android - 如何通过后台服务在android中的特定时间每天重复通知

android - 为什么在Android中不调用onReceive扩展BroadcastReceiver

Android SoundPool声音播放时断时续

android - AlarmManager 在模拟器中触发但不在物理设备上触发

java - MediaRecorder 启动失败错误

android - 更新到 Android 8.0 或 8.1 后,仅适用于 Galaxy 设备的 Cordova 应用程序触摸事件出现 ANR