gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:47:45 25 4
gpt4 key购买 nike

如何在下图中提示用户以这种方式打开 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/

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