与传统的提示用户进入设置页面并启用位置服务然后再次返回的方法不同,我发现在一些最新的应用程序中,有一种更简单的方法可以实现同样的效果.

参考下面的屏幕截图,它会向用户提示一个对话框,只需单击一下即可启用定位服务,并在这些应用程序中运行.

我怎样才能达到同样的效果?

enter image description here

推荐答案

此对话框由Google Play Services中的LocationSettingsRequest.Builder个可用用户创建.

您需要向应用程序build.gradle添加依赖项:

compile 'com.google.android.gms:play-services-location:10.0.1'

然后你可以用这个简单的例子:

private void displayLocationSettingsRequest(Context context) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API).build();
    googleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(10000 / 2);

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

    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();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }
        }
    });
}

你可以找到完整的例子here.

Android相关问答推荐

在Android、Room中重新安装应用程序时,如何删除数据库中的所有条目并完全重新初始化它,包括主密钥?

如何将Hilt添加到Android Studio中的Kotlin项目中?

Jetpack编写使用自定义主题覆盖库中主题部分

如何消除脚手架和顶杆(material 3)更改 colored颜色 时的延迟?

每次重启Android时预填入Room数据库

如何将我的Android应用程序(Kotlin)中的图像分享给其他应用程序?

Android Kotlin ImageView内置于Kotlin ImageView中.适配器未按预期更新

在一列中垂直对齐两个框

Android 14预测性背部手势-闪烁的白色背景色

为什么我的 APK 中包含来自旧版 Android 支持库的类?

请求访问小部件中的位置权限

只能从同一个库组内调用成功(引用groupId=androidx.work from groupId=My Composable)

setContentView() 方法的签名

Android apk 不工作

如何在 Jetpack Compose 中设置行宽等于 TextField 的宽度?

错误:构建 react-native 应用程序时包 com.facebook.react.bridge 不存在

compose 更改列表元素但lazyColumn不更改

线圈单元测试 - 如何做到这一点?

如何使用 Kotlin 在 Android Wear(Galaxy watch 4)中继续在后台运行应用程序

如果我在网络请求中指定它们是否与判断网络功能相关