gpt4 book ai didi

android - 在不导航到设置页面的情况下打开位置服务

转载 作者:IT老高 更新时间:2023-10-28 13:04:39 28 4
gpt4 key购买 nike

与提示用户转到设置页面并启用位置服务并再次返回的传统方法不同,我注意到在一些最新应用中执行相同操作的更简单方法。

引用下面的屏幕截图,它会提示用户一键启用位置服务的对话框,它适用于这些应用程序。

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

enter image description here

最佳答案

此对话框由 LocationSettingsRequest.Builder 创建在 Google Play 服务中可用。

你需要给你的应用添加一个依赖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 - 在不导航到设置页面的情况下打开位置服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251373/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com