我使用最新版本的Google Play Services(7.0)并遵循他们指南中给出的步骤,启用了如下所示的位置对话框,其中有一个"从不"按钮.我的应用程序需要位置强制,所以我不想向用户显示"从不",因为一旦用户单击"从不",我就根本无法获得位置或再次请求位置.
由于谷歌 map 只有是和没有按钮,没有永不按钮,你知道如何实现同样的效果吗?
我的应用程序的图像
谷歌 map 的图像
我使用最新版本的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;
}
}
});
}
关于Kotlin ,请看这里:https://stackoverflow.com/a/61868985/12478830