我希望当前在前台的应用程序的任何 Activities
都能收到一条消息。
所以我要发送 LocalBroadcastManager
在我的应用程序中分发消息。然后我的想法是拥有所有其他 Activities
必须扩展的 BaseActivity
。在 onResume
的 BaseActivity
中,接收者为消息注册 ( registerReceiver
),并在 onPause
中取消注册 ( unregisterReceiver
)。
这将始终有效,但如果消息仅在 Activities
之间的转换发生且前一个 Activity
在 onPause 之后的时间段内分发时则无效
并且下一个 Activity
在它的 onResume
之前。
所以我想使用 sendStickyBroadcast
这样消息就会等到某个Activity
没有取出来,但是我看到了
This method was deprecated in API level 21.
那么如何确保当前在前台的应用程序的任何 Activity 都能收到消息。
最佳答案
This method was deprecated in API level 21.
不过,您仍然可以使用 if 一段时间。弃用不会自动使功能无法使用。
So how to ensure any of application's Activities which is currently in foreground to receive a message.
您可以切换到事件总线库,如 GreenRobot's EventBus并使 Activity 状态/生命周期完全无关
关于android - 如何确保当前在前台的任何应用程序 Activity 都能收到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34352259/