我正在项目中使用 FusedLocationProviderClient 。在onCreate方法中,我已经调用了这个片段:
private void setupFusedLocationProvider() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, locationSettingsResponse -> locationPermissionsSatisfied());
task.addOnFailureListener(this, e -> {
if (e instanceof ResolvableApiException) {
try {
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(Activity.this,
REQUEST_CHECK_LOCATION_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
//Ignore
}
}
});
}
问题在于,即使使用缓存擦除或卸载,应用程序也不会显示请求正确位置权限设置的对话框:每次都会调用 onSuccessListener (而位置提交是没有设置)。我正在使用最新的谷歌服务apis。
我的清单包含这两个:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />