我正在创建 LocationRequest 类的 locationrequest 对象,其方法用于确定我的应用程序所需的位置准确度级别。
private LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Run Code Online (Sandbox Code Playgroud)
然后创建一个 LocationSettingsRequest.Builder 对象,然后向其中添加位置请求对象。
new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
Run Code Online (Sandbox Code Playgroud)
根据 Android 文档,SettingsClient 负责确保设备的系统设置针对应用程序的位置需求进行了正确配置。
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task =
client.checkLocationSettings(builder.build());
Run Code Online (Sandbox Code Playgroud)
文档指出,当任务完成时,客户端可以通过查看 LocationSettingsResponse 对象的状态代码来检查位置设置。
task.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) {
Log.v(" Failed ", String.valueOf(exception.getStatusCode()));
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location …Run Code Online (Sandbox Code Playgroud)