Reputation: 1697
Hi all Android geeks out there,
I am developing an Android application targetting Android Oreo (O).
minSdkVersion 21
targetSdkVersion 27
I know there are limits for running background services, and I am able to overcome them by using startForeground(...)
method of service.
My doubt is;
Should I use this startForeground(...)
method for all android versions? Since calling this method will display a notification I would like to avoid it in pre-Oreo versions if possible.
So, to avoid displaying notifications in pre-Oreo devices can I use the code snippet below? Will it work in the background for both pre and post Oreo?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O /* 26 */) {
// Make the service as foreground service by calling startForeground method
} else {
// Nothing to do
}
To summarize my question:
targetSdkVersion 27
and minSdkVersion 21
MyService
should be run in background in both pre Oreo and post Oreo OS versionsstartForeground(...)
for only post Oreo OS versionUpvotes: 0
Views: 312
Reputation: 13
WorkManager is intended for tasks that require a guarantee that the system will run them, even if the app exits Start foreground looks well when your use case requires notification also ex: Backup, navigation, location updates. Such operation start foreground fits perfectly. But there are other cases when it doesn't have to show notification. Plus it should be always used when task is deferrable.
WorkManager uses an underlying job dispatching service when available based on the following criteria: Uses JobScheduler for API 23+ Uses a custom AlarmManager + BroadcastReceiver implementation for API 14-22
link for more https://developer.android.com/reference/androidx/work/WorkManager?hl=en
Upvotes: 0
Reputation: 3034
MyService should run in the background in both pre Oreo and post Oreo OS versions. Is it okay to limit the usage of startForeground(...) for only post Oreo OS version?
Yes it is ok to use it like this.
The Service
will run in the background in pre-oreo
devices, but if the user kills your app, the Service
will also die, unless you use startForeground
for pre-oreo as well.
Upvotes: 1