This is my sample code for prompting the user for to enable GPS location.
private void showLocationSettingsRequest(Context context) {
try {
/* Initiate Google API Client */
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(SPPlaysetScanActivity.this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, "showLocationSettingsRequest :PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Log.d(TAG, "showLocationSettingsRequest :Location settings are inadequate,
// and cannot be fixed here. Dialog not created.");
break;
}
}
});
} catch (Exception e) {
Log.d(TAG, "showLocationSettingsRequest EX:" + e.toString());
}
}
If we disable location from status bar setting we are able to get the location settings dialog but when we turn on location (from the top status bar) but still the location dialog appears. I need to dismiss dialog when the location is turned on from status bar settings.
Thanks in Advance!
This is not possible on your code's side. Calling:
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
You are opening separate Activity
(from Google Play Services library). This code should be intelligent enough register BroadcastReceiver
for location change and then return result to your Activity
if GPS has been enabled.
As you can't change this code I guess what you need to do is write a change request on Google Issue tracker site: https://source.android.com/setup/report-bugs