简体   繁体   中英

Using AlarmManager for periodic task Android

I am new to Android. I want to develop an app that is going to do something every 20 minutes: if the user is in the app, they just get a message, else, the app will set a dialog and if the user accepts that dialog, the app will open and the user will get that message.

I have searched how to do that and ended up using alarm manager and everything went fine. However, the question is that if using alarm manager is good for this situation. If not, why? And what is the solution? I had read somewhere that work manager is also good.

WorkManager will not be useful in a case like this when device enters Doze mode .

WorkManager adheres to power-saving features and best practices like Doze mode

I have seen that even after white listing the app, (removing from battery optimisation), if device is left unused, stationary, unplugged, WorkManager work is delayed until the next maintenance window, sometimes hours of delays.

AlarmManager can be used but documentation recommends

Exact alarms should only be used for user-facing features. Learn more about the acceptable use cases for setting an exact alarm.

FCM is another option that could be considered in doze mode.

Edit: WorkManager is definitely recommended for persistent onetime or periodic works which are not time sensitive, where combination of constraints can be applied.

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