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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-04-14 UTC."],[[["After registering a fence, your app needs to add a callback to respond when the fence is triggered."],["Callbacks for fences are implemented using a subclass of `BroadcastReceiver` to manage `Intent` methods."],["The `BroadcastReceiver.onReceive()` method within the subclass handles `Intent` methods from the fences your app has created."],["The `FenceState.extract()` method is used to retrieve the fence state from a received `Intent` and provide it to the callback."]]],["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"]]