如果用户选择了“从不"选项,则为Android启用位置服务 [英] Enable location services for Android if user chose Never option

查看:144
本文介绍了如果用户选择了“从不"选项,则为Android启用位置服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户先前选择了从不"选项,是否有任何方法可以再次为应用允许位置服务(或至少显示对话框)?

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?

由于我总是在onResult(..)回调中获取LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE,因此我在代码中找不到再次显示它的方法:

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.

推荐答案

如果用户先前选择了"从不"选项,我没有找到任何启​​用位置服务的解决方法,所以我关注@上面的Lesvmac答案要求用户再次 删除并重新安装 应用程序,但是我认为这不是解决此问题的正确方法.

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 .

因此,目前最好的方法是不允许"Never"选项出现在请求对话框中.尝试添加

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

builder.setAlwaysShow(true);

LocationSettingsRequest.Builder 只会显示""和""选项,而不是默认的"从不","" & "不立即"&您将永远不会收到 SETTINGS_CHANGE_UNAVAILABLE

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

这是完整的方法:

 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

之后

这篇关于如果用户选择了“从不"选项,则为Android启用位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆