Me encuentro realizando una app de "Tracking" o seguimiento, para obtener la localización utilizo LocationListener dentro de un servicio, ejecuto la app en el dispositivo pero en ocasiones puedo percatarme de que la ubicación es algo errática e incluso en ocasiones no recibe actualizaciones (las coordenadas se mantienen en cero), he estado leyendo sobre otros métodos para obtener la ubicación (FusedLocationProvider), quisiera saber si puedo realizar esto con LocationListener y la mejor manera de hacerlo o si es mas conveniente utilizar como alternativa FusedLocationProvider.
Este es el código de mi servicio:
public class LocationService extends Service implements LocationListener {
private final String DEBUG_TAG = "GPSPING";
private LocationManager lm;
private double latitude;
private double longitude;
private double accuracy;
private Location location;
private boolean isEnabled = false;
@Override
public void onLocationChanged(Location location) {
Log.d(DEBUG_TAG, "onLocationChanged");
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
}
@Override
public void onProviderDisabled(String provider) {
Log.d(DEBUG_TAG, "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.d(DEBUG_TAG, "onProviderEnabled");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(DEBUG_TAG, "onStatusChanged");
}
@Override
public void onCreate() {
Log.d(DEBUG_TAG, "onCreate");
}
@Override
public void onDestroy() {
lm.removeUpdates(this);
Log.d(DEBUG_TAG, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(DEBUG_TAG, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(final Intent intent, int startid) {
Log.d(DEBUG_TAG, "onStart");
Location location = getLocation();
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d(DEBUG_TAG, "Location Lat:"+latitude +"Long: "+longitude);
}
public Location getLocation() {
try {
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
if (isNetworkEnabled) {
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
1000,
10f, this);
Log.d("Network", "Network Enabled");
if (lm != null) {
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
10f, this);
Log.d("GPS", "GPS Enabled");
if (lm != null) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}