简体   繁体   中英

Storing files with an AlarmManager and BroadcastReceiver

I have an app that does a task every minute and then adds that info to an ArrayList stored in the MainActivity of the app. While the app is running, even in the background, this works perfectly. However when I destroy the app and let the AlarmManager repeat every minute in the background, for some reason the list is wiped each time new info is added, meaning that the next time I open the app there is always only one object in the ArrayList, which was the last one created by the last AlarmManager that went off.

This event every minute is handled in a BroadcastReceiver which calls a method in the main activity to add to the arraylist stored there. Could this possibly be why my data is wiped whenever the app isn't running? Each time I want to add new data to the list, I have the app fetch the current list from storage, which fetches the list correctly until the app isn't running, then it fetches an empty list each time.

when your app is in background then your arraylist will be empty.you should store your arralist into sharedpreferences.so whenever you have to retrieve your data then you can fetch data from shareprefernces.

please check this below link:

reference link

i hope its useful to you.

Alarm Manager will not dismiss previously set alarm when your app close. So it will invoke as per last set time. So, Please make sure when you close the app, cancel previously set alarm and you can start that alarm on app launch or as per your need.

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