Reputation: 405
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
?
Upvotes: 2
Views: 3920
Reputation: 11
Unfortunately, you cannot.
This answer and android's documentation are very misleading! The foreground work can be performed ONLY if started while your app is running. Even if you set all the foreground permissions/services in the manifest, you cannot start let's say a periodic foreground worker and expect it to run in the foreground while the app is closed every 6 hours. It will succeed to run only if it happens that the app is open when the worker wakes up. (and yes, there are the exceptions if your app is a system app, the preferred messaging app, the device has just booted up, etc. — not your case).
The worker's error when the app is closed will be something like that:
2023-12-07 12:48:24.595 5362-5384 WM-Processor fr.esgi.firstfm.debug I Moving WorkSpec (eee6d859-109e-48c7-b957-95ab48d82ef8) to the foreground
2023-12-07 12:48:24.611 631-2173 ActivityManager system_server W Background started FGS: Disallowed [callingPackage: fr.esgi.firstfm.debug; callingUid: 10169; uidState: CEM ; intent: Intent { act=ACTION_START_FOREGROUND cmp=fr.esgi.firstfm.debug/androidx.work.impl.foreground.SystemForegroundService (has extras) }; code:DENIED; tempAllowListReason:<null>; targetSdkVersion:33; callerTargetSdkVersion:33; startForegroundCount:0; bindFromPackage:null]
2023-12-07 12:48:24.612 631-2173 ActivityManager system_server W startForegroundService() not allowed due to mAllowStartForeground false: service fr.esgi.firstfm.debug/androidx.work.impl.foreground.SystemForegroundService
But when the app is running, the worker will succeed:
2023-12-07 18:58:30.247 24071-24111 WM-Processor fr.esgi.firstfm.debug I Moving WorkSpec (3cf8c83f-2971-4bc9-8b05-bdff019b0dca) to the foreground
2023-12-07 18:58:30.296 24071-24071 WM-SystemFgDispatcher fr.esgi.firstfm.debug I Started foreground service Intent { act=ACTION_START_FOREGROUND cmp=fr.esgi.firstfm.debug/androidx.work.impl.foreground.SystemForegroundService (has extras) }
Upvotes: 1
Reputation: 301
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
Upvotes: 3