Stay organized with collections
Save and categorize content based on your preferences.
A fence defines one or more context conditions to which your app can react.
When a fence's state changes, your app receives a callback.
There are two types of fences: primitive fences, which represent the basic set of context
signals, and combination fences, which combine multiple primitive fences with the
use of boolean operators. All fences are instances of AwarenessFence.
Create a primitive fence
Primitive fences, which represent the basic set of context signals, are defined
in the awareness.fence
package. The following example shows the creation of a simple fence that's TRUE
when the user's detected activity is WALKING,
and FALSE otherwise:
In the preceding example, the DetectedActivityFence
was created by a call to during,
which means the fence is in the TRUE state whenever the user is WALKING.
React to transitions
Each primitive fence type, with the exception of TimeFence, can also be
triggered momentarily when the context state transitions. For example, you can
set a DetectedActivityFence to momentarily trigger when a user is
starting
or
stopping
an activity. Transition fences are in the TRUE state for a few seconds before
they turn FALSE again.
Create a combination fence
Combination fences combine multiple primitive fence types with the use of boolean
operators. The following example shows the creation of a combination fence that
activates when the user walks and the headphones are plugged in:
// Create the primitive fences.AwarenessFencewalkingFence=DetectedActivityFence.during(DetectedActivityFence.WALKING);AwarenessFenceheadphoneFence=HeadphoneFence.during(HeadphoneState.PLUGGED_IN);// Create a combination fence to AND primitive fences.AwarenessFencewalkingWithHeadphones=AwarenessFence.and(walkingFence,headphoneFence);
Nested trees of AND, OR and NOT are valid, so any boolean
combination of fences is possible. The following example shows a fence that's
triggered when a user moves more than 100 meters from the current location,
or over an hour has elapsed since the current time.
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));
[[["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-09-04 UTC."],[[["\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)."]]