No se puede recuperar la ubicación usando 'LocationManager.NETWORK_PROVIDER' incluso cuando WiFi está habilitado en un dispositivo con Android M
Estoy tratando de recuperar la ubicación usandoLocationManager
siguiendoesta tutorial.
Estoy ejecutando el código en un dispositivo Android con Marshmallow y he verificado el permiso y ejecuto el siguiente código una vez que se otorga el permiso.
Aquí está mi código:
public Location getLocation() {
try {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
Log.d("noProviderEnabled", "called");
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
currentLatDouble = location.getLatitude();
currentLngDouble = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
currentLatDouble = location.getLatitude();
currentLngDouble = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
Aunque WiFi esté habilitado,isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
está volviendofalse
yLog.d("noProviderEnabled", "called");
se imprime
Actualizar: cuando encendí elGPS
y luego ejecute el código anterior ... solo elisNetworkEnabled
regresadotrue
yisGPSEnabled
todavía regresófalse
.
¿Qué pasa aquí? ¿Por qué no puedo recuperar la ubicación usandoLocationManager.NETWORK_PROVIDER
incluso cuando WiFi está habilitado?
P.S .: Esto funciona perfectamente bien en Android L y versiones inferiores.