管理栅栏回调
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注册边界后,您的应用必须添加回调,以便在边界触发时做出响应。为此,您可以使用 BroadcastReceiver
的子类来处理围栏中的 Intent
方法。
在向栅栏添加回调之前,您必须先注册栅栏。
创建 BroadcastReceiver 的子类
以下示例展示了扩展了 BroadcastReceiver
的 FenceReceiver
类。该类会实现 BroadcastReceiver.onReceive()
回调方法,以处理源自应用创建的栅栏的所有 Intent
方法。收到 Intent
时,系统会使用 FenceState.extract()
方法获取栅栏状态并将其传递给回调。
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);
}
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\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 }"]]