android - 如何要求用户打开位置

标签 android android-studio google-maps

如何在下图中提示用户以这种方式打开 gps。我尝试使用警报对话框方法,但它进入了我希望打开 gps 的设置 View ,而不像大多数应用程序那样移动到设置 Activity 。我怎样才能实现它enter image description here

最佳答案

这适用于 java AndroidX 2020 更新:

private void enableLoc() {



    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);


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

    builder.setAlwaysShow(true);

    Task<LocationSettingsResponse> result =
            LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());

    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {


        @Override
        public void onComplete(Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse response = task.getResult(ApiException.class);
                // All location settings are satisfied. The client can initialize location
                // requests here.

            } catch (ApiException exception) {
                switch (exception.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the
                        // user a dialog.
                        try {
                            // Cast to a resolvable exception.
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            resolvable.startResolutionForResult(
                                    Maps.this,
                                    LOCATION_SETTINGS_REQUEST);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        } catch (ClassCastException e) {
                            // Ignore, should be an impossible error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }
            }
        }
    });

}

关于android - 如何要求用户打开位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61339719/

相关文章:

javascript - google maps api V3 需要 key 吗?

android - 如何在我的代码中查看所有可能的空指针异常 lints?

java - 无法将数据添加到非 Activity 类中声明的 HashMap

java - 使用 MediaRecorder 在 Android 上录制屏幕时无法获取 Surface

java - 这个小Java程序中的错误

android - MvvmCross + Fragments + Back Button = 崩溃或控件不响应/绑定(bind)

android - 为什么即使我的手机支持 A-GPS,Android GPS 在打开谷歌地图之前也无法找到位置

android - 我们可以用边框包裹 ExpandableListView 的 child 吗

Android 泄漏金丝雀 - 泄漏空 Activity

java - 如何使用 RxJava 发出错误然后发出缓存数据?