Gestisci callback recinzioni
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Una volta registrato un recinto virtuale, l'app deve aggiungere un callback per rispondere quando viene attivato. Puoi farlo utilizzando una sottoclasse di
BroadcastReceiver
per gestire i metodi Intent
delle recinti.
Prima di aggiungere i callback a una recinzione, devi prima
registrarla.
Crea una sottoclasse di BroadcastReceiver
L'esempio seguente mostra la classe FenceReceiver
, che estendeBroadcastReceiver
. La classe implementa il metodo callback BroadcastReceiver.onReceive()
per gestire tutti i metodi Intent
che provengono dai recinti creati dalla tua app. Quando viene ricevuto un Intent
, viene utilizzato il metodo FenceState.extract()
per ottenere lo stato del recinto e passarlo al callback.
public class FenceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
FenceState fenceState = FenceState.extract(intent);
if (TextUtils.equals(fenceState.getFenceKey(), FENCE_KEY)) {
String fenceStateStr;
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
fenceStateStr = "true";
break;
case FenceState.FALSE:
fenceStateStr = "false";
break;
case FenceState.UNKNOWN:
fenceStateStr = "unknown";
break;
default:
fenceStateStr = "unknown value";
}
mLogFragment.getLogView().println("Fence state: " + fenceStateStr);
}
}
}
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-08-31 UTC.
[null,null,["Ultimo aggiornamento 2025-08-31 UTC."],[[["\u003cp\u003eAfter registering a fence, your app needs to add a callback to respond when the fence is triggered.\u003c/p\u003e\n"],["\u003cp\u003eCallbacks for fences are implemented using a subclass of \u003ccode\u003eBroadcastReceiver\u003c/code\u003e to manage \u003ccode\u003eIntent\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eBroadcastReceiver.onReceive()\u003c/code\u003e method within the subclass handles \u003ccode\u003eIntent\u003c/code\u003e methods from the fences your app has created.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eFenceState.extract()\u003c/code\u003e method is used to retrieve the fence state from a received \u003ccode\u003eIntent\u003c/code\u003e and provide it to the callback.\u003c/p\u003e\n"]]],["After registering a fence, a callback must be added to respond when it's triggered. This is achieved by creating a subclass of `BroadcastReceiver`. The `FenceReceiver` class, extending `BroadcastReceiver`, implements the `onReceive()` method. Upon receiving an `Intent` from a fence, `FenceState.extract()` retrieves the fence state. The code then checks the fence key and current state (`TRUE`, `FALSE`, or `UNKNOWN`) to update the log view. This allows the app to react to fence state changes.\n"],null,["# Manage fence callbacks\n\nOnce a fence is registered, your app must add a callback to respond\nwhen the fence is triggered. You can do this with the use of a subclass of\n`BroadcastReceiver` to handle `Intent` methods from fences.\n\nBefore you add callbacks to a fence, you must first\n[register](/awareness/android-api/fence-register) the fence.\n\nCreate a subclass of BroadcastReceiver\n--------------------------------------\n\nThe following example shows the `FenceReceiver` class, which extends\n`BroadcastReceiver`. The class implements the\n`BroadcastReceiver.onReceive()` callback method to handle all `Intent` methods\nthat originate from fences created by your app. When an `Intent` is received, the\n[`FenceState.extract()`](/android/reference/com/google/android/gms/awareness/fence/FenceState#extract(android.content.Intent))\nmethod is used to get the fence state and pass it to the callback. \n\n public class FenceReceiver extends BroadcastReceiver {\n\n @Override\n public void onReceive(Context context, Intent intent) {\n\n FenceState fenceState = FenceState.extract(intent);\n\n if (TextUtils.equals(fenceState.getFenceKey(), FENCE_KEY)) {\n String fenceStateStr;\n switch (fenceState.getCurrentState()) {\n case FenceState.TRUE:\n fenceStateStr = \"true\";\n break;\n case FenceState.FALSE:\n fenceStateStr = \"false\";\n break;\n case FenceState.UNKNOWN:\n fenceStateStr = \"unknown\";\n break;\n default:\n fenceStateStr = \"unknown value\";\n }\n mLogFragment.getLogView().println(\"Fence state: \" + fenceStateStr);\n }\n }\n }"]]