启用 GPS 的 LocationSettingsRequest 对话框

问题描述

过去几天我一直在寻找解决方案,但找不到任何解决方案。我正在处理为 android 9+ 关闭谷歌位置精度的情况,然后应用程序应提示用户切换位置精度。

面临的问题:

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

以下是片段。

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 {

        }

    }
});
 

解决方法

试试这个,对我有用

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();
}

你可以在这里调用它。 enter image description here

然后就是这个效果。

enter image description here