Destroy listeners after fetching loclization

This commit is contained in:
Mieszko Wrzeszczyński 2018-12-18 21:43:15 +01:00
parent fa0b69920f
commit 956cd83f9b

View File

@ -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 {