Gérer les rappels pour les clôtures
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Une fois une clôture enregistrée, votre application doit ajouter un rappel pour répondre lorsque la clôture est déclenchée. Pour ce faire, utilisez une sous-classe de BroadcastReceiver
pour gérer les méthodes Intent
à partir de barrières.
Avant d'ajouter des rappels à une clôture, vous devez d'abord l'enregistrer.
Créer une sous-classe de BroadcastReceiver
L'exemple suivant montre la classe FenceReceiver
, qui étend BroadcastReceiver
. La classe implémente la méthode de rappel BroadcastReceiver.onReceive()
pour gérer toutes les méthodes Intent
provenant des barrières créées par votre application. Lorsqu'un Intent
est reçu, la méthode FenceState.extract()
est utilisée pour obtenir l'état de la barrière et le transmettre au rappel.
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);
}
}
}
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/31 (UTC).
[null,null,["Dernière mise à jour le 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 }"]]