简体   繁体   中英

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. 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. We also have the method for setting a notification in 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. 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. The context I get in the onReceive method returns different context, so it can't be accessed through that. 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.

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.

So is it possible to somehow get the context of mainActivity in BroadcastReceiver? 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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