จัดการโค้ดเรียกกลับของรั้ว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อลงทะเบียนรั้วแล้ว แอปของคุณต้องเพิ่มการเรียกกลับเพื่อตอบสนองเมื่อรั้วทริกเกอร์ ซึ่งทำได้โดยใช้คลาสย่อยของ
BroadcastReceiver
เพื่อจัดการเมธอด Intent
จากรั้ว
คุณต้องลงทะเบียนรั้วก่อนจึงจะเพิ่มการเรียกกลับได้
สร้างคลาสย่อยของ BroadcastReceiver
ตัวอย่างต่อไปนี้แสดงคลาส FenceReceiver
ซึ่งขยายจาก BroadcastReceiver
คลาสนี้ใช้
BroadcastReceiver.onReceive()
เมธอด Callback เพื่อจัดการกับเมธอด Intent
ทั้งหมดซึ่งมาจากรั้วที่แอปของคุณสร้างขึ้น เมื่อได้รับ Intent
ระบบจะใช้เมธอด FenceState.extract()
เพื่อรับสถานะรั้วและส่งไปยัง Callback
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\u003eAfter registering a fence, your app needs to add a callback to respond when the fence is triggered.\u003c/p\u003e\n"],["\u003cp\u003eCallbacks for fences are implemented using a subclass of \u003ccode\u003eBroadcastReceiver\u003c/code\u003e to manage \u003ccode\u003eIntent\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eBroadcastReceiver.onReceive()\u003c/code\u003e method within the subclass handles \u003ccode\u003eIntent\u003c/code\u003e methods from the fences your app has created.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eFenceState.extract()\u003c/code\u003e method is used to retrieve the fence state from a received \u003ccode\u003eIntent\u003c/code\u003e and provide it to the callback.\u003c/p\u003e\n"]]],["After registering a fence, a callback must be added to respond when it's triggered. This is achieved by creating a subclass of `BroadcastReceiver`. The `FenceReceiver` class, extending `BroadcastReceiver`, implements the `onReceive()` method. Upon receiving an `Intent` from a fence, `FenceState.extract()` retrieves the fence state. The code then checks the fence key and current state (`TRUE`, `FALSE`, or `UNKNOWN`) to update the log view. This allows the app to react to fence state changes.\n"],null,["# Manage fence callbacks\n\nOnce a fence is registered, your app must add a callback to respond\nwhen the fence is triggered. You can do this with the use of a subclass of\n`BroadcastReceiver` to handle `Intent` methods from fences.\n\nBefore you add callbacks to a fence, you must first\n[register](/awareness/android-api/fence-register) the fence.\n\nCreate a subclass of BroadcastReceiver\n--------------------------------------\n\nThe following example shows the `FenceReceiver` class, which extends\n`BroadcastReceiver`. The class implements the\n`BroadcastReceiver.onReceive()` callback method to handle all `Intent` methods\nthat originate from fences created by your app. When an `Intent` is received, the\n[`FenceState.extract()`](/android/reference/com/google/android/gms/awareness/fence/FenceState#extract(android.content.Intent))\nmethod is used to get the fence state and pass it to the callback. \n\n public class FenceReceiver extends BroadcastReceiver {\n\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 }"]]