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); 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) { if (!stopService) {
mStatusChecker = () -> { mStatusChecker = () -> {
try { try {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
initializeLocationManager();
getLocalizationFromListeners(); getLocalizationFromListeners();
changeBackgroundMode(); changeBackgroundMode();
} finally { } finally {
mFusedLocationClient = null;
destroyLocationListeners();
mHandler.postDelayed(mStatusChecker, notify_interval); mHandler.postDelayed(mStatusChecker, notify_interval);
} }
}; };
@ -175,6 +156,7 @@ public class BackgroundLocalizationService extends Service {
} else if (shouldAbbreviateTimeInterval) { } else if (shouldAbbreviateTimeInterval) {
notify_interval = notify_interval_inside_building; notify_interval = notify_interval_inside_building;
} }
} }
} }
@ -252,7 +234,10 @@ public class BackgroundLocalizationService extends Service {
super.onDestroy(); super.onDestroy();
mHandler.removeCallbacks(mStatusChecker); mHandler.removeCallbacks(mStatusChecker);
destroyLocationListeners();
}
private void destroyLocationListeners(){
if (mLocationManager != null) { if (mLocationManager != null) {
for (LocationListener listener : mLocationListeners) { for (LocationListener listener : mLocationListeners) {
try { try {
@ -269,11 +254,31 @@ public class BackgroundLocalizationService extends Service {
} }
private void initializeLocationManager() { 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) { if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 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 { private class LocationListener implements android.location.LocationListener {