Enable location services for Android if user chose

2019-04-04 11:49发布

问题:

Is there any way to allow location services again for an app (or at least show the dialog) if the user previously chose the option Never?

I dont find any way in code to show it again, since i always get LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE in the onResult(..) callback:

    @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(activity, 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;
        }
    }

The only solution i found is to reinstall the app or clear data.

回答1:

I didn't find any workaround to enable location services if earlier the user had chosen "Never" option, so I followed @Lesvmac answer's above to ask user again to delete and reinstall app, but this I think isn't correct way to solve this problem around .

So at present best way is to not allow "Never" option to appear in request dialog. Try to add

builder.setAlwaysShow(true);

to LocationSettingsRequest.Builder which will result in only "Yes" and "No" option instead of default "Never", "Yes" & "Not Now" & you will never receive SETTINGS_CHANGE_UNAVAILABLE

Here's the full method:

 private void requestSettings() {
    LocationSettingsRequest.Builder builder =
            new LocationSettingsRequest.Builder()
                    .setAlwaysShow(true)
                    .addLocationRequest(request);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                    builder.build());

    result.setResultCallback(this);
}

Before

After



回答2:

Right, never means never! If the user previously answered "not now" to your app's location permission request, you can produce a dialog asking permission. But if they have selected to never let you ask again, that is set permanently and you have to tell them to delete and reinstall. (In the case that their location is temporarily turned off, but the app has permission to access their location, then a dialog can be produced asking to change the location services status for the device.) If location is a necessary component of the app, you may consider requiring access at install. In the upcoming Android M, individual permissions can be set and reset for each app, helping to avoid this sort of problem.



回答3:

You need to manually allow the permission on that specific app to reset the request permission dialog.

Open the App info (either by dragging from app launcher, or Settings - Apps - [app name])

Select Permissions

Enable the permission you denied with "never ask again"

(Optional) Disable it again from here; note that by this time, your app will request the permission again when needed.

Link: https://android.stackexchange.com/questions/125939/reset-the-request-permission-dialog-on-marshmallow



回答4:

Solution is very easy and very easy to miss (on Android 5.1 at least) ...

Swipe down from top screen edge to get pull down menu. Press on "App permission management". Press Apps and the app you wish to alter Permissions for. Voila!