From 57b82e9a183cce82656751cb05d93d67e65a2962 Mon Sep 17 00:00:00 2001 From: Hennie Brink Date: Fri, 14 Oct 2022 11:12:57 +0200 Subject: [PATCH] Android location stream not cancelling (#1153) * Fixes a bug where the Android services does not shut down correctly * Update foreground notification description --- geolocator_android/CHANGELOG.md | 4 ++++ .../baseflow/geolocator/GeolocatorLocationService.java | 8 +++++++- .../java/com/baseflow/geolocator/StreamHandlerImpl.java | 8 ++++---- geolocator_android/lib/src/types/android_settings.dart | 6 ++++++ geolocator_android/pubspec.yaml | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/geolocator_android/CHANGELOG.md b/geolocator_android/CHANGELOG.md index bfa03271..bdf27642 100644 --- a/geolocator_android/CHANGELOG.md +++ b/geolocator_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.1.4 + +- Fix a bug where the location service would not stop correctly when the location stream is cancelled. + ## 4.1.3 - Export `AndroidResource` class at `geolocator_android.at`. diff --git a/geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorLocationService.java b/geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorLocationService.java index c82b5c32..356ac41d 100644 --- a/geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorLocationService.java +++ b/geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorLocationService.java @@ -36,6 +36,7 @@ public class GeolocatorLocationService extends Service { // Service is foreground private boolean isForeground = false; private int connectedEngines = 0; + private int listenerCount = 0; @Nullable private Activity activity = null; @Nullable private GeolocationManager geolocationManager = null; @Nullable private LocationClient locationClient; @@ -83,7 +84,10 @@ public void onDestroy() { super.onDestroy(); } - public boolean canStopLocationService() { + public boolean canStopLocationService(boolean cancellationRequested) { + if(cancellationRequested) { + return listenerCount == 1; + } return connectedEngines == 0; } @@ -104,6 +108,7 @@ public void startLocationService( LocationOptions locationOptions, EventChannel.EventSink events) { + listenerCount++; if (geolocationManager != null) { locationClient = geolocationManager.createLocationClient( @@ -121,6 +126,7 @@ public void startLocationService( } public void stopLocationService() { + listenerCount--; Log.d(TAG, "Stopping location service."); if (locationClient != null && geolocationManager != null) { geolocationManager.stopPositionUpdates(locationClient); diff --git a/geolocator_android/android/src/main/java/com/baseflow/geolocator/StreamHandlerImpl.java b/geolocator_android/android/src/main/java/com/baseflow/geolocator/StreamHandlerImpl.java index b46827fb..2c0a983d 100644 --- a/geolocator_android/android/src/main/java/com/baseflow/geolocator/StreamHandlerImpl.java +++ b/geolocator_android/android/src/main/java/com/baseflow/geolocator/StreamHandlerImpl.java @@ -81,7 +81,7 @@ void stopListening() { return; } - disposeListeners(); + disposeListeners(false); channel.setStreamHandler(null); channel = null; } @@ -145,12 +145,12 @@ public void onListen(Object arguments, EventChannel.EventSink events) { @Override public void onCancel(Object arguments) { - disposeListeners(); + disposeListeners(true); } - private void disposeListeners() { + private void disposeListeners(boolean cancelled) { Log.e(TAG, "Geolocator position updates stopped"); - if (foregroundLocationService != null && foregroundLocationService.canStopLocationService()) { + if (foregroundLocationService != null && foregroundLocationService.canStopLocationService(cancelled)) { foregroundLocationService.stopLocationService(); foregroundLocationService.disableBackgroundMode(); } else { diff --git a/geolocator_android/lib/src/types/android_settings.dart b/geolocator_android/lib/src/types/android_settings.dart index 2ca8a9b3..fea5dfb0 100644 --- a/geolocator_android/lib/src/types/android_settings.dart +++ b/geolocator_android/lib/src/types/android_settings.dart @@ -50,6 +50,12 @@ class AndroidSettings extends LocationSettings { /// If this is set then the services is started as a Foreground service with a persistent notification /// showing the user that the service will continue running in the background. + /// + /// Note: Using this foreground notification does not run your service in the background, it just + /// increases the priority of your activity making it less likely for Android to kill the activity + /// when switching between apps. It does not prevent Android from killing the activity. If you want to + /// receive background location updates even if the activity is destroyed you need to use a third party + /// background service package that will start a new Flutter Engine that is not tied to your main activity. final ForegroundNotificationConfig? foregroundNotificationConfig; /// Set to true if altitude should be calculated as MSL (EGM2008) from NMEA messages diff --git a/geolocator_android/pubspec.yaml b/geolocator_android/pubspec.yaml index 9e6d027a..f50e81d9 100644 --- a/geolocator_android/pubspec.yaml +++ b/geolocator_android/pubspec.yaml @@ -2,7 +2,7 @@ name: geolocator_android description: Geolocation plugin for Flutter. This plugin provides the Android implementation for the geolocator. repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_android issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen -version: 4.1.3 +version: 4.1.4 environment: sdk: ">=2.15.0 <3.0.0"