创建围栏
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
边界用于定义应用可以响应的一个或多个上下文条件。当栅栏的状态发生变化时,您的应用会收到回调。
栅栏有两种类型:基元栅栏,表示一组基本上下文信号;组合栅栏,使用布尔运算符组合多个基元栅栏。所有围栏都是 AwarenessFence
的实例。
创建基元栅栏
基元屏障(表示一组基本情境信号)在 awareness.fence
软件包中定义。以下示例展示了如何创建一个简单的围栏,当用户的检测到的活动为 WALKING
时为 TRUE
,否则为 FALSE
:
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
在前面的示例中,DetectedActivityFence
是通过调用 during
创建的,这意味着每当用户处于 WALKING
状态时,栅栏便处于 TRUE
状态。
响应转场
除了 TimeFence
之外,每个基元栅栏类型也可以在上下文状态转换时暂时触发。例如,您可以设置 DetectedActivityFence
,以便在用户starting
或stopping
某个 activity 时暂时触发。转换栅栏会在 TRUE
状态下停留几秒钟,然后再次变为 FALSE
。
创建组合围栏
组合栅栏使用布尔运算符组合多个基元栅栏类型。以下示例展示了如何创建组合围栏,该围栏会在用户步行和插入耳机时激活:
// Create the primitive fences.
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
AwarenessFence headphoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);
// Create a combination fence to AND primitive fences.
AwarenessFence walkingWithHeadphones = AwarenessFence.and(
walkingFence, headphoneFence
);
AND
、OR
和 NOT
的嵌套树是有效的,因此可以使用任何布尔值组合来构建围栏。以下示例展示了一个围栏,当用户离开当前位置超过 100 米,或当前时间过去超过一小时时,系统就会触发该围栏。
double currentLocationLat; // current location latitude
double currentLocationLng; // current location longitude
long nowMillis = System.currentTimeMillis();
long oneHourMillis = 1L * 60L * 60L * 1000L;
AwarenessFence orExample = AwarenessFence.or(
AwarenessFence.not(LocationFence.in(
currentLocationLat,
currentLocationLng,
100.0,
100.0,
0L)),
TimeFence.inInterval(nowMillis + oneHourMillis, Long.MAX_VALUE));
后续步骤:注册围栏。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eFences define context conditions that trigger callbacks in your app when their state changes, with each fence being an instance of \u003ccode\u003eAwarenessFence\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThere are two primary fence types: primitive fences, representing basic context signals, and combination fences, which merge multiple primitive fences using boolean operators.\u003c/p\u003e\n"],["\u003cp\u003ePrimitive fences can be configured to react to the duration of a context state (e.g., \u003ccode\u003eWALKING\u003c/code\u003e) or to the transition between states (e.g., starting or stopping an activity).\u003c/p\u003e\n"],["\u003cp\u003eCombination fences allow for the creation of complex conditions by using \u003ccode\u003eAND\u003c/code\u003e, \u003ccode\u003eOR\u003c/code\u003e, and \u003ccode\u003eNOT\u003c/code\u003e operators to combine multiple primitive fences, such as a user walking \u003cem\u003eand\u003c/em\u003e having headphones plugged in.\u003c/p\u003e\n"],["\u003cp\u003eNested boolean operations of \u003ccode\u003eAND\u003c/code\u003e, \u003ccode\u003eOR\u003c/code\u003e and \u003ccode\u003eNOT\u003c/code\u003e are supported, allowing the creation of elaborate conditions, such as a user being further than 100 meters from a location, or one hour has elapsed.\u003c/p\u003e\n"]]],["Fences define context conditions, triggering callbacks upon state changes. Primitive fences represent basic context signals (e.g., `WALKING`), while combination fences use boolean operators to combine multiple primitive fences. Fences can be created to represent a state (e.g., `during WALKING`) or a transition (e.g., `starting` or `stopping` an activity). `AND`, `OR`, and `NOT` operators create complex combination fences to manage sophisticated awareness logic, such as being within a radius and having a specific time. All fences are `AwarenessFence` instances.\n"],null,["# Create a fence\n\nA fence defines one or more context conditions to which your app can react.\nWhen a fence's state changes, your app receives a callback.\n\nThere are two types of fences: primitive fences, which represent the basic set of context\nsignals, and combination fences, which combine multiple primitive fences with the\nuse of boolean operators. All fences are instances of [`AwarenessFence`](/android/reference/com/google/android/gms/awareness/fence/AwarenessFence).\n\nCreate a primitive fence\n------------------------\n\nPrimitive fences, which represent the basic set of context signals, are defined\nin the [`awareness.fence`](/android/reference/com/google/android/gms/awareness/fence/package-summary)\npackage. The following example shows the creation of a simple fence that's `TRUE`\nwhen the user's detected activity is [`WALKING`](/android/reference/com/google/android/gms/awareness/fence/DetectedActivityFence#WALKING),\nand `FALSE` otherwise: \n\n AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);\n\nIn the preceding example, the [`DetectedActivityFence`](/android/reference/com/google/android/gms/awareness/fence/DetectedActivityFence)\nwas created by a call to [`during`](/android/reference/com/google/android/gms/awareness/fence/DetectedActivityFence#during(int...)),\nwhich means the fence is in the `TRUE` state whenever the user is `WALKING`.\n\n### React to transitions\n\nEach primitive fence type, with the exception of `TimeFence`, can also be\ntriggered momentarily when the context state transitions. For example, you can\nset a `DetectedActivityFence` to momentarily trigger when a user is\n[`starting`](/android/reference/com/google/android/gms/awareness/fence/DetectedActivityFence#starting(int...))\nor\n[`stopping`](/android/reference/com/google/android/gms/awareness/fence/DetectedActivityFence#stopping(int...))\nan activity. Transition fences are in the `TRUE` state for a few seconds before\nthey turn `FALSE` again.\n\nCreate a combination fence\n--------------------------\n\nCombination fences combine multiple primitive fence types with the use of boolean\noperators. The following example shows the creation of a combination fence that\nactivates when the user walks *and* the headphones are plugged in: \n\n // Create the primitive fences.\n AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);\n AwarenessFence headphoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);\n\n // Create a combination fence to AND primitive fences.\n AwarenessFence walkingWithHeadphones = AwarenessFence.and(\n walkingFence, headphoneFence\n );\n\nNested trees of `AND`, `OR` and `NOT` are valid, so any boolean\ncombination of fences is possible. The following example shows a fence that's\ntriggered when a user moves more than 100 meters from the current location,\n*or* over an hour has elapsed since the current time. \n\n double currentLocationLat; // current location latitude\n double currentLocationLng; // current location longitude\n long nowMillis = System.currentTimeMillis();\n long oneHourMillis = 1L * 60L * 60L * 1000L;\n\n AwarenessFence orExample = AwarenessFence.or(\n AwarenessFence.not(LocationFence.in(\n currentLocationLat,\n currentLocationLng,\n 100.0,\n 100.0,\n 0L)),\n TimeFence.inInterval(nowMillis + oneHourMillis, Long.MAX_VALUE));\n\nNext step: [Register a fence](/awareness/android-api/fence-register)."]]