如需查询地理围栏的当前状态,请调用
FenceClient.queryFences()并传递要查询的地理围栏的地理围栏键。
以下示例调用 FenceClient.queryFences() 以获取 FenceStateMap,
然后使用 FenceStateMap 返回
FenceState
值,以显示当前状态、先前状态以及地理围栏上次更新的时间:
protected void queryFence(final String fenceKey) {
Awareness.getFenceClient(this)
.queryFences(FenceQueryRequest.forFences(Arrays.asList(fenceKey)))
.addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {
@Override
public void onSuccess(FenceQueryResponse response) {
FenceStateMap map = response.getFenceStateMap();
for (String fenceKey : map.getFenceKeys()) {
FenceState fenceState = map.getFenceState(fenceKey);
Log.i(TAG, "Fence " + fenceKey + ": "
+ fenceState.getCurrentState()
+ ", was="
+ fenceState.getPreviousState()
+ ", lastUpdateTime="
+ DATE_FORMAT.format(
new Date(fenceState.getLastFenceUpdateTimeMillis())));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Could not query fence: " + fenceKey);
return;
}
});
}