From 956cd83f9bae215fbb53f984e31d6ec5d435b0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Tue, 18 Dec 2018 21:43:15 +0100 Subject: [PATCH] Destroy listeners after fetching loclization --- .../BackgroundLocalizationService.java | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java index 0417d17..6b1ef41 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java @@ -124,36 +124,17 @@ public class BackgroundLocalizationService extends Service { startForeground(1001, notification); } - mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); - - initializeLocationManager(); - - Integer providerIndex = 0; - - for (LocationListener listener : mLocationListeners) { - try { - mLocationManager.requestLocationUpdates( - providers.get(providerIndex), - LOCATION_INTERVAL, - LOCATION_DISTANCE, - listener - ); - - } catch (java.lang.SecurityException ex) { - Log.i(TAG, "fail to request location update, ignore", ex); - } catch (IllegalArgumentException ex) { - Log.d(TAG, "network provider does not exist, " + ex.getMessage()); - } - - providerIndex++; - } if (!stopService) { mStatusChecker = () -> { try { + mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); + initializeLocationManager(); getLocalizationFromListeners(); changeBackgroundMode(); } finally { + mFusedLocationClient = null; + destroyLocationListeners(); mHandler.postDelayed(mStatusChecker, notify_interval); } }; @@ -175,6 +156,7 @@ public class BackgroundLocalizationService extends Service { } else if (shouldAbbreviateTimeInterval) { notify_interval = notify_interval_inside_building; } + } } @@ -252,7 +234,10 @@ public class BackgroundLocalizationService extends Service { super.onDestroy(); mHandler.removeCallbacks(mStatusChecker); + destroyLocationListeners(); + } + private void destroyLocationListeners(){ if (mLocationManager != null) { for (LocationListener listener : mLocationListeners) { try { @@ -269,11 +254,31 @@ public class BackgroundLocalizationService extends Service { } private void initializeLocationManager() { - Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE); + Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + notify_interval + " LOCATION_DISTANCE: " + LOCATION_DISTANCE); if (mLocationManager == null) { mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); } + + Integer providerIndex = 0; + + for (LocationListener listener : mLocationListeners) { + try { + mLocationManager.requestLocationUpdates( + providers.get(providerIndex), + notify_interval, + LOCATION_DISTANCE, + listener + ); + + } catch (java.lang.SecurityException ex) { + Log.i(TAG, "fail to request location update, ignore", ex); + } catch (IllegalArgumentException ex) { + Log.d(TAG, "network provider does not exist, " + ex.getMessage()); + } + + providerIndex++; + } } private class LocationListener implements android.location.LocationListener {