【问题标题】:LocationSettingsRequest dialog to enable GPS启用 GPS 的 LocationSettingsRequest 对话框
【发布时间】:2021-05-08 23:37:11
【问题描述】:

从过去几天开始,我一直在寻找解决方案,但找不到任何解决方案。我正在处理一种情况,即为 android 9+ 关闭了 google 位置精度,然后应用程序应提示用户打开位置精度。

面临的问题:

如果 (statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) 是否可以直接导航到设备的谷歌位置准确度屏幕(看起来很困难)。 自定义由谷歌服务库提示的对话框(我认为这是不可能的)。任何想法将不胜感激。

下面是sn-p。

mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();     mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(mActivity,
        new OnSuccessListener<LocationSettingsResponse>() {
    @Override
    public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
        mFusedProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {


            }
        });
    }
}).addOnFailureListener(mActivity, new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        int statusCode = ((ApiException) e).getStatusCode();
        if(statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED){
            //Location settings not satisfied.
            try {

                ResolvableApiException resolvable = (ResolvableApiException) e;
                resolvable.startResolutionForResult(
                        mActivity,
                        LOCATION_REQUEST);
            } catch (IntentSender.SendIntentException ex) {
    
            }
        } else {

        }

    }
});
 

【问题讨论】:

    标签: android location android-gps google-location-services


    【解决方案1】:

    试试这个,对我有用

    private void showLocationDialog() {
        new AlertDialog.Builder(RequestLocationUpdatesWithIntentActivity.this).setTitle("The location service is not enabled on the phone.")
                .setMessage("Go to Settings > Location information (enable location service).")
                .setNegativeButton("cancels", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton("Unset", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        try {
                            RequestLocationUpdatesWithIntentActivity.this.startActivity(intent);
                        } catch (ActivityNotFoundException ex) {
                            intent.setAction(Settings.ACTION_SETTINGS);
                            try {
                                RequestLocationUpdatesWithIntentActivity.this.startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                })
                .show();
    }
    

    你可以在这里调用它。

    然后就是这个效果了。

    【讨论】:

    • 非常感谢。你是最棒的:)
    • 嗨,这有帮助吗?如果对您有帮助,请接受此答案。谢谢:)
    猜你喜欢
    • 2015-09-22
    • 2016-01-22
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2019-11-23
    • 2016-09-30
    相关资源
    最近更新 更多