📌  相关文章
📜  以编程方式打开设备位置 android - 无论代码示例

📅  最后修改于: 2022-03-11 14:57:43.094000             🧑  作者: Mango

代码示例2
protected void enableLocationSettings() {
        LocationRequest locationRequest = LocationRequest.create()
                .setInterval(10 * 1000)
                .setFastestInterval(2 * 1000)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

        LocationServices
                .getSettingsClient(requireActivity())
                .checkLocationSettings(builder.build())
                .addOnSuccessListener(requireActivity(), (LocationSettingsResponse response) -> {
                    // startUpdatingLocation(...);
                })
                .addOnFailureListener(requireActivity(), ex -> {
                    if (ex instanceof ResolvableApiException) {
                        try{
                            IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(((ResolvableApiException) ex).getResolution()).build();
                            resolutionForResult.launch(intentSenderRequest);
                        }catch (Exception exception){
                            Log.d(TAG, "enableLocationSettings: "+exception);
                        }
                    }
                });
    }