2

Google describe in their blog post a one touch settings dialog that asks the user to turn on their location (letting the user turn on location without sending them to their phone's settings). I just cannot find the appropiate method / approach to do this in their API docs.

Anyone already using this and can give an explanation?

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • i had been waiting for this option, since they put that out.... http://stackoverflow.com/questions/28759454/enabling-location-with-mode-high-accuracy-or-battery-saving-without-user-needing – Pararth May 11 '15 at 09:38

2 Answers2

5

I think you can check sample project by google

Tse Ka Leong
  • 408
  • 4
  • 20
2

More specific, relevant code is below

class MainActivity extends Activity implements ResultCallback<LocationSettingsResult> {
    protected void checkLocationSettings() {
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(
                        mGoogleApiClient,
                        mLocationSettingsRequest
                );
        result.setResultCallback(this);
    }

    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                        "upgrade location settings ");
                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        }
    }
}
// This code is from https://github.com/googlesamples/android-play-location/blob/master/LocationSettings/app/src/main/java/com/google/android/gms/location/sample/locationsettings/MainActivity.java

In the result callback of LocationSettingsStatusCodes#RESOLUTION_REQUIRED, Status.startResolutionForResult will do the trick.

Kazuki
  • 1,462
  • 14
  • 34