Issue
I have to create an application that activates the proximity alert at certain times of the day (this period of time can be several hours long). The proximity alert must be started automatically, even if the application has never been opened.
My question is: can I use the WorkManager
in this situation? or should I fall back on the foreground service
?
Solution
WorkManager 2.3.0-alpha02 adds built-in support for long running workers. In such cases, WorkManager can provide a signal to the OS that the process should be kept alive if possible while this work is executing. These Workers can run longer than 10 minutes. Example use-cases for this new feature include bulk uploads or downloads (that cannot be chunked), crunching on an ML model locally, or a task that's important to the user of the app.
Under the hood, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, while also showing a configurable notification.
ListenableWorker now supports the setForegroundAsync() API, and CoroutineWorker supports a suspending setForeground() API. These APIs allow developers to specify that this WorkRequest is important (from a user perspective) or long-running.
Starting with 2.3.0-alpha03, WorkManager also allows you to create a PendingIntent, which can be used to cancel workers without having to register a new Android component using the createCancelPendingIntent() API. This approach is especially useful when used with the setForegroundAsync() or setForeground() APIs, which can be used to add a notification action to cancel the Worker.
Link to the resource: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running
Answered By - Nursultan Almakhanov
Answer Checked By - Pedro (JavaFixing Volunteer)