在上次更新com.google.android.gms:播放服务位置到21.0.0版本之前,我可以使用下一个代码激活GPS设备:
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(Priority.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(Priority.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnFailureListener((Activity) context, e -> {
if (e instanceof ResolvableApiException) {
try {
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult((Activity) context,
23);
} catch (IntentSender.SendIntentException sendEx) {
sendEx.printStackTrace();
}
}
});
更新到版本21后,我的代码看起来很像:
LocationRequest locationRequest =
new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.setWaitForAccurateLocation(false)
.setIntervalMillis(10000)
.setPriority(Priority.PRIORITY_HIGH_ACCURACY)
.build();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnFailureListener((Activity) context, e -> {
if (e instanceof ResolvableApiException) {
try {
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult((Activity) context,
23);
} catch (IntentSender.SendIntentException sendEx) {
sendEx.printStackTrace();
}
}
});
你怎么能看到locationRequest的修饰符用builder((替换create((?
问题是GPS激活菜单再也不会显示了。看起来新的异常是不可解析的ApiException。
知道我如何再次使用启用GPS消息吗?
我不想使用设置方式:
((ActivityHome)context).startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
最新版本有这个问题,请选择更低的版本。
dependencies
{
implementation 'com.google.android.gms:play-services-location:21.0.0'
}
更改为…
dependencies
{
implementation 'com.google.android.gms:play-services location:20.0.0'
}