Untuk membuat kueri status fence saat ini, panggil FenceClient.queryFences()
dan teruskan kunci fence untuk fence untuk membuat kueri.
Contoh berikut memanggil FenceClient.queryFences()
untuk mendapatkan FenceStateMap
,
lalu menggunakan FenceStateMap
untuk menampilkan nilai
FenceState
guna menampilkan status saat ini, status sebelumnya, dan waktu fence
terakhir diperbarui:
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;
}
});
}