用于启用 GPS 的 LocationSettingsRequest 对话框

LocationSettingsRequest dialog to enable GPS

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

面临的问题:

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

下面是片段。

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

你可以在这里调用它。

然后就是这个效果