フェンスの作成
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
フェンスは、アプリが反応できるコンテキスト条件を 1 つ以上定義します。フェンスの状態が変化すると、アプリはコールバックを受け取ります。
フェンスには、コンテキスト シグナルの基本セットを表すプリミティブ フェンスと、ブール演算子を使用して複数のプリミティブ フェンスを組み合わせる組み合わせフェンスの 2 種類があります。すべてのフェンスは AwarenessFence
のインスタンスです。
プリミティブ フェンスを作成する
プリミティブ フェンス(コンテキスト シグナルの基本セットを表すもの)は、awareness.fence
パッケージで定義されています。次の例は、ユーザーの検出されたアクティビティが WALKING
の場合に TRUE
、それ以外の場合は FALSE
となるシンプルなフェンスを作成する方法を示しています。
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
上の例では、during
の呼び出しによって DetectedActivityFence
が作成されています。つまり、ユーザーが WALKING
の場合、フェンスは常に TRUE
状態になります。
遷移に反応する
TimeFence
を除く各プリミティブ フェンス タイプは、コンテキストの状態が遷移したときに一時的にトリガーすることもできます。たとえば、ユーザーがアクティビティをstarting
またはstopping
しているときに、DetectedActivityFence
を一時的にトリガーするように設定できます。遷移フェンスは、数秒間 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 メートル以上移動したとき、または現在の時刻から 1 時間以上経過したときにトリガーされるフェンスを示しています。
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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 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)."]]