I am using following code to show popup to turn on location
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
SettingsClient client = LocationServices.getSettingsClient(getActivity());
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
getUserLocation();
}
});
task.addOnFailureListener(getActivity(), new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
Log.d("AMOD", "onFailure " + statusCode);
switch (statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(getActivity(),
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way
// to fix the settings so we won't show the dialog.
break;
}
}
});
Following is code for It showing popup as below
But sometimes even if user clicks Ok I am getting resposne 0 i.e RESULT_CANCEL this happening after updating play services to 16.0.0
Reported this bug on google issuetracker as well following is link for more details on it
onActivityResult
and start NETWORK_PROVIDER && GPS_PROVIDER with result OK, and some times it result canceled and start device only location mode. this is my question #53388241 – Worrywart