简体   繁体   English

设置大量通知的最有效方法是什么?

[英]What is the most efficient way to set a lot of notifications?

I am creating a calendar app where you can set unlimited amount of activities which trigger alarmManager at start.我正在创建一个日历应用程序,您可以在其中设置无限量的活动,这些活动在启动时触发 alarmManager。 And I am not sure what would be the best way to set the alarms.而且我不确定设置警报的最佳方法是什么。

Lets say we have 50.000 alarms which starting times are loaded from the database and save in array in MainActivity and we need to set them.假设我们有 50.000 个警报,它们的开始时间从数据库中加载并保存在 MainActivity 的数组中,我们需要设置它们。 We also have the method for setting a notification in MainActivity.我们还有在 MainActivity 中设置通知的方法。

The way I wanted to do it was to set first (closest) alarm and in the broadcastReceiver I would set the next following alarm.我想做的方式是设置第一个(最近的)警报,在 broadcastReceiver 中,我将设置下一个以下警报。 But this idea didn't work because I have the list and method is in mainActivity and so I needed to get the instance of that mainActivity.但是这个想法没有奏效,因为我的列表和方法在 mainActivity 中,所以我需要获取那个 mainActivity 的实例。 The context I get in the onReceive method returns different context, so it can't be accessed through that.我在 onReceive 方法中获得的上下文返回不同的上下文,因此无法通过它访问它。 I also looked for a way to pass activity instance in an intent but found nothing.我还寻找了一种在意图中传递活动实例的方法,但一无所获。 Lastly I tried to create interface in the broadcastReceiver class and pass the instance somehow this way but it was a dead end as well.最后,我尝试在 broadcastReceiver class 中创建接口,并以这种方式传递实例,但这也是一个死胡同。

The other option that came to my mind was to set the first X alarms on start in the main thread and then the rest in separate thread but that doesn't seem much efficient.我想到的另一个选择是在主线程中设置第一个 X 警报,然后在单独的线程中设置 rest,但这似乎效率不高。

So is it possible to somehow get the context of mainActivity in BroadcastReceiver?那么是否有可能以某种方式获得 BroadcastReceiver 中 mainActivity 的上下文? If not what's the most efficient way to set a lot of notifications?如果不是设置大量通知的最有效方法是什么?

Appreciate all the help感谢所有帮助

Some time flew by and as @CommonsWare and @Richard Onslow Roper suggested I was able to come up with a correct solution.一段时间过去了,正如@CommonsWare 和@Richard Onslow Roper 建议的那样,我能够想出一个正确的解决方案。

As suggested I created new repository where to store the notifications.正如建议的那样,我创建了新的存储库来存储通知。 When the first notification is triggered, meaning we're in BroadcastReceiver.class onReceive method, it will set the notification and then queue the next by scheduling new work in workManager.当第一个通知被触发时,意味着我们在 BroadcastReceiver.class onReceive 方法中,它将设置通知,然后通过在 workManager 中安排新工作来排队下一个通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM