Better provider location management.

This commit is contained in:
Chteufleur 2018-08-15 17:21:25 +02:00
parent 4e17bc3800
commit 8855235fd8
1 changed files with 26 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package fr.chteufleur.mytrackingdog.services;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
@ -68,7 +69,12 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
@Override
public void start() {
try {
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
// if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
Log.i(TAG, "Start location");
// } else {
// Log.i(TAG, "Start location failed (provider not enabled)");
// }
} catch (SecurityException ex) {
Log.e(TAG, "Location permission is not activated.", ex);
}
@ -77,11 +83,13 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
@Override
public void stop() {
this.locationManager.removeUpdates(this);
Log.i(TAG, "Stop location");
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged");
currentLocation = new MyLocation(location);
traces.addCurrentPoint(currentLocation);
setChanged();
@ -90,17 +98,31 @@ public class ServiceGps extends Observable implements IServiceGps, LocationListe
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
stop();
Log.i(TAG, "onStatusChanged("+s+", "+i+", null)");
if (s != null && s.equals(LocationManager.GPS_PROVIDER)) {
if (i == LocationProvider.AVAILABLE) {
start();
} else if (i == LocationProvider.TEMPORARILY_UNAVAILABLE) {
} else if (i == LocationProvider.OUT_OF_SERVICE) {
stop();
}
}
}
@Override
public void onProviderEnabled(String s) {
start();
Log.i(TAG, "onProviderEnabled("+s+")");
if (s != null && s.equals(LocationManager.GPS_PROVIDER)) {
start();
}
}
@Override
public void onProviderDisabled(String s) {
stop();
Log.i(TAG, "onProviderDisabled("+s+")");
if (s != null && s.equals(LocationManager.GPS_PROVIDER)) {
stop();
}
}
public void toggleTraceurActivation() {