从活动的BroadcastReceiver和Android上区分适用对象适用、对象、BroadcastReceiver、Android

2023-09-12 04:44:34 作者:不痛不痒不在乎

我有依靠推送通知更新事件发生网络用户的应用程序。要做到这一点,我有我的正常活动,其显示游戏的用户,然后一个BroadcastReceiver它的存在是为了处理传入的推送消息。

I have an application that relies on push notifications to update users of events happening online. To do so, I have my normal Activity which displays the game to the user, and then a BroadcastReceiver which exists to process incoming push messages.

不过,每当我们收到消息,它创建了我们的应用程序的一个实例。在我们的Application.onCreate()函数中,我们进行了一些preloading和处理逻辑当创建的应用程序是实际的活动,而不是的BroadcastReceiver应该只运行。

However, whenever we receive a message, it creates an instance of our Application. In our Application.onCreate() function we perform some preloading and processing logic that should only run when the Application being created is the actual Activity and not the BroadcastReceiver.

我也试图将处理和逻辑入Activity.onCreate(),但整个生命周期要少得多predictable比的应用,所以应用程序的方法似乎是preferred,但我愿意接受替代解决方案。

I also tried to move the processing and logic into the Activity.onCreate() but that whole lifecycle is much less predictable than that of the Application, so the Application method seems to be preferred, but I am open to alternate solutions.

我如何能够确定应用程序实例正在为的BroadcastReceiver或活动?

How can I determine if the Application instance is being created for the BroadcastReceiver or Activity?

推荐答案

我通过移动昂贵的preloading一个静态初始化的活动解决了这个问题。

I solved this issue by moving the expensive preloading to a static initializer on the Activity.