查询栅栏状态

如需查询栅栏的当前状态,请调用 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;
            }
        });
}