Consulta para o estado de cerca
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Para consultar o estado atual de um limite, chame
FenceClient.queryFences()
e transmita a chave do limite para a consulta.
O exemplo a seguir chama FenceClient.queryFences()
para receber um FenceStateMap
e, em seguida, usa o FenceStateMap
para retornar um
valor FenceState
para mostrar o estado atual, o estado anterior e o horário em que a cerca foi
atualizada pela última vez:
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;
}
});
}
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
Última atualização 2025-08-31 UTC.
[null,null,["Última atualização 2025-08-31 UTC."],[[["\u003cp\u003eUse \u003ccode\u003eFenceClient.queryFences()\u003c/code\u003e to get the current state of a fence by providing its fence key.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equeryFences()\u003c/code\u003e method returns a \u003ccode\u003eFenceStateMap\u003c/code\u003e containing the state of the queried fences.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFenceState\u003c/code\u003e objects within the map provide the current, previous state, and last update time of each fence.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code example demonstrates querying for a fence's state and logging its details.\u003c/p\u003e\n"]]],[],null,["# Query for fence state\n\nTo query for the current state of a fence, call\n[`FenceClient.queryFences()`](/android/reference/com/google/android/gms/awareness/FenceClient#public-taskfencequeryresponse-queryfences-fencequeryrequest-fencequeryrequest) and pass the fence key for the fence to query.\n\nThe following example calls `FenceClient.queryFences()` to get a `FenceStateMap`,\nand then uses the `FenceStateMap` to return a\n[`FenceState`](/android/reference/com/google/android/gms/awareness/fence/FenceState)\nvalue to show the current state, previous state, and the time when the fence was\nlast updated: \n\n protected void queryFence(final String fenceKey) {\n Awareness.getFenceClient(this)\n .queryFences(FenceQueryRequest.forFences(Arrays.asList(fenceKey)))\n .addOnSuccessListener(new OnSuccessListener\u003cFenceQueryResponse\u003e() {\n @Override\n public void onSuccess(FenceQueryResponse response) {\n FenceStateMap map = response.getFenceStateMap();\n for (String fenceKey : map.getFenceKeys()) {\n FenceState fenceState = map.getFenceState(fenceKey);\n Log.i(TAG, \"Fence \" + fenceKey + \": \"\n + fenceState.getCurrentState()\n + \", was=\"\n + fenceState.getPreviousState()\n + \", lastUpdateTime=\"\n + DATE_FORMAT.format(\n new Date(fenceState.getLastFenceUpdateTimeMillis())));\n }\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n Log.e(TAG, \"Could not query fence: \" + fenceKey);\n return;\n }\n });\n }"]]