Manage fence callbacks
Stay organized with collections
Save and categorize content based on your preferences.
Once a fence is registered, your app must add a callback to respond
when the fence is triggered. You can do this with the use of a subclass of
BroadcastReceiver
to handle Intent
methods from fences.
Before you add callbacks to a fence, you must first
register the fence.
Create a subclass of BroadcastReceiver
The following example shows the FenceReceiver
class, which extends
BroadcastReceiver
. The class implements the
BroadcastReceiver.onReceive()
callback method to handle all Intent
methods
that originate from fences created by your app. When an Intent
is received, the
FenceState.extract()
method is used to get the fence state and pass it to the 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);
}
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 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 }"]]