สร้างรั้ว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
รั้วกั้นจะกําหนดเงื่อนไขบริบทอย่างน้อย 1 รายการที่แอปสามารถตอบสนองได้
เมื่อสถานะของรั้วมีการเปลี่ยนแปลง แอปของคุณจะได้รับการเรียกกลับ
รั้วมี 2 ประเภท ได้แก่ รั้วพื้นฐาน ซึ่งแสดงชุดสัญญาณบริบทพื้นฐาน และรั้วแบบผสม ซึ่งรวมรั้วพื้นฐานหลายรายการเข้าด้วยกันโดยใช้ตัวดำเนินการบูลีน รั้วทั้งหมดเป็นอินสแตนซ์ของ AwarenessFence
สร้างรั้วแบบพื้นฐาน
รั้วพื้นฐานซึ่งแสดงชุดสัญญาณบริบทพื้นฐานจะกำหนดไว้ในแพ็กเกจ awareness.fence
ตัวอย่างต่อไปนี้แสดงการสร้างรั้วแบบง่ายที่ TRUE
เมื่อกิจกรรมที่ตรวจพบของผู้ใช้คือ WALKING
และ FALSE
ในกรณีอื่นๆ
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
ในตัวอย่างก่อนหน้านี้ DetectedActivityFence
สร้างขึ้นโดยการเรียกใช้ during
ซึ่งหมายความว่ารั้วจะอยู่ในสถานะ TRUE
ทุกครั้งที่ผู้ใช้อยู่ในสถานะ WALKING
รีแอ็กต่อการเปลี่ยนฉาก
รั้วประเภทพื้นฐานแต่ละประเภท (ยกเว้น TimeFence
) สามารถทริกเกอร์ได้ชั่วขณะเมื่อสถานะบริบทมีการเปลี่ยนแปลง เช่น คุณสามารถตั้งค่า DetectedActivityFence
ให้ทริกเกอร์เป็นระยะเวลาสั้นๆ เมื่อผู้ใช้starting
หรือstopping
กิจกรรม รั้วการเปลี่ยนสถานะจะอยู่ในสถานะ TRUE
เป็นเวลา 2-3 วินาทีก่อนที่จะเปลี่ยนเป็น 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)."]]