I have a code that makes location requests for the user, everything works fine as long as the user is already in the "location" mode is active, however, if the "location" mode is not activated in the device notification bar location returns null, I know that when we disable the option the cache of locations is cleared, how could I ask the user to activate the option for q then I request the getLastLocation?
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
localidade = task.getResult();
if(localidade != null){
try {
locFromLatLon = new Geocoder(AddAnuncioActivity.this, Locale.getDefault());
List<Address> addresses = locFromLatLon.getFromLocation(localidade.getLatitude(), localidade.getLongitude(), 1);
Log.i("VALORZAS", addresses.get(0).getCountryName());
} catch (IOException e) {
e.printStackTrace();
}
}else{
// Put some here whenlocation is null -- this happens when the Location option in notification bar of // user is unabled
}
}
});
This only work if the option from the image is enabled, if this option is off, the getResult returns null.
My onResume function:
@Override
protected void onResume() {
super.onResume();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(5000);
LocationCallback locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
Toast.makeText(getApplicationContext(), "Localidade ainda vazia", Toast.LENGTH_LONG).show();
return;
}
for (Location location : locationResult.getLocations()) {
}
}
};
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
}