diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt index 8090015ec1..2ab2c91411 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.general.automation import android.content.Intent +import android.os.Build import android.os.Handler import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R @@ -65,7 +66,10 @@ object AutomationPlugin : PluginBase(PluginDescription() override fun onStart() { val context = MainApp.instance().applicationContext - context.startService(Intent(context, LocationService::class.java)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + context.startForegroundService(Intent(context, LocationService::class.java)) + else + context.startService(Intent(context, LocationService::class.java)) super.onStart() loadFromSP() @@ -76,9 +80,11 @@ object AutomationPlugin : PluginBase(PluginDescription() .observeOn(Schedulers.io()) .subscribe({ e -> if (e.isChanged(R.string.key_location)) { - val ctx = MainApp.instance().applicationContext - ctx.stopService(Intent(ctx, LocationService::class.java)) - ctx.startService(Intent(ctx, LocationService::class.java)) + context.stopService(Intent(context, LocationService::class.java)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + context.startForegroundService(Intent(context, LocationService::class.java)) + else + context.startService(Intent(context, LocationService::class.java)) } }, { FabricPrivacy.logException(it) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java index 9dcb800033..e06dc741be 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java @@ -123,7 +123,7 @@ public class PersistentNotificationPlugin extends PluginBase { } @Nonnull - Notification updateNotification() { + public Notification updateNotification() { String line1 = null; String line2 = null; String line3 = null; diff --git a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java index e7caa89626..feec4dcab8 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventLocationChange; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; @@ -76,9 +77,10 @@ public class LocationService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + super.onStartCommand(intent, flags, startId); if (L.isEnabled(L.LOCATION)) log.debug("onStartCommand"); - super.onStartCommand(intent, flags, startId); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification()); return START_STICKY; }