ภาพรวมของ Fence API
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ใน Awareness API แนวคิดรั้วมาจากการกำหนดเขตพื้นที่เสมือน ซึ่งมีการกําหนดภูมิภาคทางภูมิศาสตร์หรือเขตพื้นที่เสมือน และแอปจะได้รับการเรียกกลับเมื่อผู้ใช้เข้าหรือออกจากภูมิภาคเขตพื้นที่เสมือน Fence API ขยายแนวคิดของการกำหนดเขตพื้นที่เสมือนให้รวมเงื่อนไขอื่นๆ อีกมากมายนอกเหนือจากความใกล้ชิดทางภูมิศาสตร์ แอปจะได้รับ Callback ทุกครั้งที่บริบทมีการเปลี่ยนแปลงสถานะ ตัวอย่างเช่น หากแอปกำหนดรั้วสำหรับหูฟัง แอปจะได้รับการเรียกกลับเมื่อเสียบปลั๊กหูฟังและเมื่อถอดปลั๊ก
คุณสามารถใช้ Fence API เพื่อกําหนดรั้วตามสัญญาณบริบท เช่น ต่อไปนี้
- ตำแหน่งปัจจุบันของผู้ใช้ (ละติจูด/ลองจิจูด)
- กิจกรรมปัจจุบันของผู้ใช้ เช่น การเดินหรือการขับรถ
- เงื่อนไขเฉพาะอุปกรณ์ เช่น มีการเสียบปลั๊กหูฟังหรือไม่
- ระยะทางจากบีคอนที่อยู่ใกล้เคียง
Fence API ช่วยให้คุณรวมสัญญาณบริบทหลายรายการเข้าด้วยกันเพื่อสร้างรั้วด้วยโอเปอเรเตอร์บูลีน AND
, OR
และ NOT
จากนั้นแอปของคุณจะได้รับการเรียกกลับทุกครั้งที่เป็นไปตามเงื่อนไขของรั้ว ตัวอย่างรั้วที่เป็นไปได้มีดังนี้
- ผู้ใช้เสียบหูฟังและเริ่มเดิน
- ผู้ใช้เข้าไปในเขตพื้นที่เสมือนขนาด 100 เมตรก่อนเวลา 17:00 น. ในวันธรรมดา
- ผู้ใช้เข้าสู่ระยะสัญญาณของบีคอน BLE ที่เฉพาะเจาะจง
ตัวอย่างต่อไปนี้แสดงวิธีกำหนดรั้วที่จะเปิดใช้งานทุกครั้งที่ผู้ใช้เดิน
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
เมื่อกำหนดรั้วแล้ว คุณต้องดำเนินการต่อไปนี้
- โทรหา
updateFences
เพื่อลงทะเบียนรั้วเพื่อรับการโทรกลับ
- กำหนดการเรียกกลับที่เรียกใช้ได้เมื่อสถานะรั้วไฟฟ้ามีการเปลี่ยนแปลง
ตัวอย่างต่อไปนี้แสดงเมธอดที่สร้างและลงทะเบียนรั้ว ในตัวอย่างนี้ มีการใช้คลาสย่อยที่กำหนดเองของ BroadcastReceiver
เพื่อจัดการ Intent เมื่อรั้วทริกเกอร์
Awareness.getFenceClient(this).updateFences(new FenceUpdateRequest.Builder()
.addFence(FENCE_KEY, exercisingWithHeadphonesFence, mPendingIntent)
.build())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Fence was successfully registered.");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Fence could not be registered: " + e);
}
});
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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe Awareness API uses fences, inspired by geofencing, to trigger app callbacks based on various context signals like location, user activity, and device conditions.\u003c/p\u003e\n"],["\u003cp\u003eYou can define fences using the Fence API by combining context signals with boolean operators (AND, OR, NOT) to create specific conditions.\u003c/p\u003e\n"],["\u003cp\u003eApps receive callbacks when a fence's conditions are met, such as when a user starts walking while wearing headphones or enters a designated area.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize fences, register them using \u003ccode\u003eupdateFences\u003c/code\u003e and define a callback (e.g., using a \u003ccode\u003eBroadcastReceiver\u003c/code\u003e) to handle fence state changes.\u003c/p\u003e\n"]]],["The Fence API expands upon geofencing by allowing apps to define fences based on various context conditions. These include user location, activity, device status, and proximity to beacons. Fences can combine multiple context signals using boolean operators, triggering callbacks when conditions are met. To use a fence, you define it, register it via `updateFences`, and create a callback (like a `BroadcastReceiver`) to respond to state changes. The callback's state will reflect `TRUE`, `FALSE`, or `UNKNOWN`.\n"],null,["# Fence API overview\n\nIn the Awareness API, the concept of *fences* is taken from\n[geofencing](https://developer.android.com/training/location/geofencing),\nin which a geographic region, or *geofence*, is defined, and an app receives\ncallbacks when a user enters or leaves the geofence region. The Fence API expands on\nthe concept of geofencing to include many other context conditions in addition\nto geographical proximity. An app receives callbacks whenever the context\nstate transitions. For example, if your app defines a fence for headphones,\nit gets callbacks when the headphones are plugged in and when they're\nunplugged.\n\nYou can use the\n[Fence API](/android/reference/com/google/android/gms/awareness/Awareness#getFenceClient(android.app.Activity))\nto define fences based on context signals, such as the following:\n\n- The user's current location (latitude/longitude)\n- The user's current activity, like walking or driving.\n- Device-specific conditions, such as whether the headphones are plugged in.\n- Proximity to nearby beacons\n\nThe Fence API lets you combine multiple\n[context signals](/awareness/overview#context-types)\nto create fences with `AND`, `OR`, and `NOT` boolean operators. Your app then\nreceives callbacks whenever the fence conditions are met. Some examples of\npossible fences include the following:\n\n- User plugs in headphones and starts to walk.\n- User enters a 100-meter geofence before 5 PM on a weekday.\n- User enters range of a specific BLE beacon.\n\nThe following example shows how to define a fence that activates whenever\nthe user walks: \n\n AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);\n\nOnce you've defined a fence, you must do the following:\n\n- Call [`updateFences`](/android/reference/com/google/android/gms/awareness/FenceClient#updateFences(com.google.android.gms.awareness.fence.FenceUpdateRequest)) to register the fence to receive callbacks.\n- Define a callback that can be invoked when the fence state changes.\n\nThe following example shows a method that creates and registers a fence. In\nthis example, a custom subclass of `BroadcastReceiver` is used to handle the\nintent when the fence is triggered. \n\n Awareness.getFenceClient(this).updateFences(new FenceUpdateRequest.Builder()\n .addFence(FENCE_KEY, exercisingWithHeadphonesFence, mPendingIntent)\n .build())\n .addOnSuccessListener(new OnSuccessListener\u003cVoid\u003e() {\n @Override\n public void onSuccess(Void aVoid) {\n Log.i(TAG, \"Fence was successfully registered.\");\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n Log.e(TAG, \"Fence could not be registered: \" + e);\n }\n });\n public class FenceReceiver extends BroadcastReceiver {\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 }"]]