0

In my android app, I have a requirement where I need to execute a task at 5 days interval. The task is to call an API and get the data from the server even if the app is not running/terminated.

I have checked the potential options to do this which are as below:

  1. Alarm manager + Broadcast receiver + Intentservice - In this approach the problem is with the restrictions imposed by the Android framework for the intentService. Also, the registered Alarm will be erased if the device is rebooted.

  2. WorkManager - this seems a better approach for my task to have a guaranteed execution on the latest android versions but still it's not clear whether to use PeriodicWorkRequest or OneTimeWorkRequest and keep rescheduling it.

Please note that it's not critical to execute this task on an exact clock time but it should work after each 5 days of interval.

RKB
  • 106
  • 10

1 Answers1

1

The first option is the better approach

Alarm manager + Broadcast receiver + Servcie: Here you need to make the service a foreground service with a notification. Your service will always run even if the app is terminated or not running. For restart you can have a reboot receiver and set the alarm again. With this approach the service can run wherein a lot of time is needed say downloading of multiple files as it can run until the job finishes completely.

WorkManager: With WorkManager the main problem is that the behavior is not consistent across all mobile manufacturers. Mobile manufactures like Samsung have restrictions for battery optimization and the WorkManager would not work when the app is not running or terminated. The WorkManager is not good in situation where in a lot of time is needed say downloading of multiple files as it would get terminated after 10 minutes (if am not wrong)

Work Manager not working when app is killed by the user. Why?

akhil nair
  • 1,371
  • 1
  • 11
  • 19