| Class Info |
|---|
| public class SettingsClient 位置相关设置检查类,在调用LocationServices类的getSettingsClient(Context context)或getSettingsClient(Activity activity)方法时会返回一个该类型的实例。 |
“Java”
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
LocationRequest locationRequest = new LocationRequest();
LocationSettingsRequest.Builder builder =
new LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
.setAlwaysShow(false)
.setNeedBle(false);
settingsClient.checkLocationSettings(builder.build())
.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
if (task != null && task.isSuccessful()) {
LocationSettingsResponse response = task.getResult();
if (response == null) {
return;
}
LocationSettingsStates locationSettingsStates = response.getLocationSettingsStates();
// TODO:接口调用成功的结果处理
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "checkLocationSettings onFailure:" + e.getMessage());
}
}); “Kotlin”
val settingsClient = LocationServices.getSettingsClient(this)
val locationRequest = LocationRequest()
val builder =
LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
.setAlwaysShow(false)
.setNeedBle(false)
settingsClient.checkLocationSettings(builder.build())
.addOnCompleteListener { task ->
if (task != null && task.isSuccessful) {
val response = task.result ?: return@addOnCompleteListener
val locationSettingsStates =
response!!.locationSettingsStates
// TODO:接口调用成功的结果处理
}
}
.addOnFailureListener { e ->
Log.i(TAG, "checkLocationSettings onFailure:" + e.message)
} | Qualifier and Type | Method Name and Description |
|---|---|
| checkLocationSettings(LocationSettingsRequest locationSettingsRequest) 检查位置设置各开关是否可用。 | |
| Task<Void> | setLogConfig(LogConfig logConfig) 设置开启日志写入功能。 |
| Method |
|---|
| public Task<LocationSettingsResponse> checkLocationSettings(LocationSettingsRequest locationSettingsRequest) 检查位置设置各开关是否可用。 要使用此API,请首先创建LocationSettingsRequest.Builder并添加该应用将使用的LocationRequest: LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest); 然后调用checkLocationSettings方法: Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build()); 客户端可以通过调用getLocationSettingsStates()来获取相关位置设置的当前状态。 |
Parameters
| Name | Description |
|---|---|
| locationSettingsRequest | 位置设置相关的请求对象。 |
Returns
| Type | Description |
|---|---|
| 任务。通过添加addOnSuccessListener()和addOnFailureListener()监听,获取当前方法执行的结果。失败的情况下,需要将抛出的Exception对象强转为ApiException对象,再调用ApiException对象的getStatusCode()方法确定返回码。 6:失败(可指导用户通过修改位置设置解决) 。 |
| Method |
|---|
| public Task<Void> setLogConfig(LogConfig logConfig) 设置开启日志写入功能。 要使用此API,请首先创建SettingsClient settingsClient = LocationServices.getSettingsClient(this); 然后创建LogConfig logConfig = new LogConfig();给logConfig设置参数, 最后调用SettingsClient的setLogConfig()方法开启日志写入。 |
Parameters
| Name | Description |
|---|---|
| logConfig | 日志写入相关的请求参数对象。 |
Returns
| Type | Description |
|---|---|
| Task<Void> | 任务。通过添加addOnFailureListener()监听,获取当前方法执行的结果。失败的情况下,通过调用Exception对象的getMessage()方法,获取失败原因。当没有失败信息回调时,日志保存功能开启成功。 |