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);
}
}
}