我使用最新版本的Google Play Services(7.0)并遵循他们指南中给出的步骤,启用了如下所示的位置对话框,其中有一个"从不"按钮.我的应用程序需要位置强制,所以我不想向用户显示"从不",因为一旦用户单击"从不",我就根本无法获得位置或再次请求位置.

由于谷歌 map 只有是和没有按钮,没有永不按钮,你知道如何实现同样的效果吗?

我的应用程序的图像

谷歌 map 的图像

推荐答案

LocationSettingsRequest.Builder有一个方法setAlwaysShow(boolean show).虽然文档指示其当前不执行任何操作(更新2015-07-05:更新的Google文档已删除此措辞),但设置builder.setAlwaysShow(true);将启用Google Maps行为: 在此处输入图像描述

下面是让它工作的代码:

if (googleApiClient == null) {
    googleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    googleApiClient.connect();

    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); //this is the key ingredient
    //**************************

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                getActivity(), 1000);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the 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;
            }
        }
    });
}

From Android Documentation

关于Kotlin ,请看这里:https://stackoverflow.com/a/61868985/12478830

Android相关问答推荐

在Android应用程序上使用Room数据库时,是否可以预先填充数据

如何在Android Emulator上从物理设备接收TCP消息

如何解决Android Studio中的in fragment问题

如何将子零部件的大小调整为可以调整大小的父组件大小?

处理Room数据库中的嵌套实体

Jetpack创作动画断断续续变化的观点

Android Compose Pages 3-一次加载所有页面,无需在LazyColumn中滚动,无需网络调用和内部滚动

在Android 14/SDK 34中使用RegisterReceiver的正确方式是什么?

Jetpack编写错误(java.lang.NoSuchMethodError:无接口方法startRestartGroup)

在Jetpack Compose中实现自动换行

为什么 Android Compose 将片段作为参数传递给 Composables 函数?

参数化类RecyclerView.Adapter的原始使用

在段的中心绘制饼图(甜甜圈图)的图例

使用 Kotlin 在 Android 中导航时如何防止 ViewModel 被杀死?

LazyColumn 项目,隐式接收器无法在此上下文中调用单元

如何在屏幕旋转或系统主题更改后将光标移动到 TextField 的末尾并保持键盘显示?

更新后 Firebase 服务无法在模拟器上运行

禁用通知权限后启动前台服务导致崩溃(Android 13)

如何使用 Jetpack Compose 在应用程序中实现本地化

设备文件资源管理器-ROOM 数据库中的数据库文件夹为空