Android,如果用户拒绝一次,如何再次显示位置启用对话框

时间:2016-11-07 13:25:57

标签: android android-location

我有一个情况,我正在显示位置启用对话框,如果用户拒绝一次,我无法再次显示对话框,虽然我反复调用方法startResolutionForResult()

P.S:我没有在对话框中显示选择never的选项,所以它应该每次都显示对话框。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我使用以下例程请求位置设置对话框并且它按预期工作,这意味着如果用户拒绝它(通过点击Not Now),它会在请求时再次弹出

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();

    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest);
    result.setResultCallback(this);
}
@Override
public void onResult(@NonNull 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 {
                status.startResolutionForResult(HostActivity.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;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Log.i(TAG, "User agreed to make required location settings changes.");
                    Util.toast(HostActivity.this, R.string.requesting_location_data);
                    startLocationUpdates();
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i(TAG, "User chose not to make required location settings changes.");
                    break;
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

使用此代码我相信您可以实现相同目标。

请注意,ActivityFragment必须实施ResultCallback<LocationSettingsResult>