Durum değişikliklerini işlemek için bir PendingIntent.
Çitin kimliğini tanımlayan ve bir AwarenessFence-PendingIntent çiftiyle eşlenen bir dize olan çit anahtarı.
Aşağıdaki kod örneğinde, çit kaydetmek için updateFences()'ü çağıran bir yöntem gösterilmektedir:
Awareness.getFenceClient(this).updateFences(newFenceUpdateRequest.Builder().addFence(FENCE_KEY,exercisingWithHeadphonesFence,mPendingIntent).build()).addOnSuccessListener(newOnSuccessListener<Void>(){@OverridepublicvoidonSuccess(VoidaVoid){Log.i(TAG,"Fence was successfully registered.");}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){Log.e(TAG,"Fence could not be registered: "+e);}});
Benzersiz çit anahtarlarına sahip birden fazla çit oluşturmak için addFence()
birden çok kez çağırın. İhtiyacınız olduğu kadar PendingIntent yöntemi kullanabilirsiniz ancak tüm çit geri çağırmalarında tek bir PendingIntent kullanılması tercih edilir. addFence() çağrısı yapmak için önceden kaydedilmiş bir çit anahtarı kullanırsanız söz konusu anahtar için AwarenessFence ve PendingIntent değerlerinin üzerine yazılır.
Çit kaydını iptal etme
Bir çitin kaydını iptal etmek için getFenceClient().updateFences() numarasını arayın ve çit güncelleme isteği oluşturmak için FenceUpdateRequest.Builder() işlevini kullanın. Ardından, aşağıdaki örnekte gösterildiği gibi removeFence() işlevini çağırın:
Awareness.getFenceClient(this).updateFences(newFenceUpdateRequest.Builder().removeFence(FENCE_KEY).build()).addOnSuccessListener(newOnSuccessListener<Void>(){@OverridepublicvoidonSuccess(VoidaVoid){Log.i(TAG,"Fence was successfully unregistered.");}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){Log.e(TAG,"Fence could not be unregistered: "+e);}});
[null,null,["Son güncelleme tarihi: 2025-08-31 UTC."],[[["\u003cp\u003eYour app receives callbacks via \u003ccode\u003ePendingIntent\u003c/code\u003e when a registered fence's state changes.\u003c/p\u003e\n"],["\u003cp\u003eRegister fences using \u003ccode\u003eFenceClient\u003c/code\u003e and \u003ccode\u003eFenceUpdateRequest\u003c/code\u003e, providing an \u003ccode\u003eAwarenessFence\u003c/code\u003e, a \u003ccode\u003ePendingIntent\u003c/code\u003e, and a unique fence key.\u003c/p\u003e\n"],["\u003cp\u003eUnregister fences using \u003ccode\u003eFenceClient\u003c/code\u003e and \u003ccode\u003eFenceUpdateRequest\u003c/code\u003e with \u003ccode\u003eremoveFence()\u003c/code\u003e and the corresponding fence key.\u003c/p\u003e\n"],["\u003cp\u003eYou can manage multiple fences and their updates within a single \u003ccode\u003eFenceUpdateRequest\u003c/code\u003e for efficient handling.\u003c/p\u003e\n"]]],[],null,["# Register a fence\n\nYour app receives a callback by `PendingIntent` whenever the state of a fence\nchanges. Your app must register each fence before it can be used.\n\nRegister a fence\n----------------\n\nTo register a fence, use the\n[`FenceClient`](/android/reference/com/google/android/gms/awareness/FenceClient),\nand to construct a [`FenceUpdateRequest`](/android/reference/com/google/android/gms/awareness/FenceClient#updateFences(com.google.android.gms.awareness.fence.FenceUpdateRequest)),\nuse [`FenceClient.updateFences()`](/android/reference/com/google/android/gms/awareness/FenceClient#updateFences(com.google.android.gms.awareness.fence.FenceUpdateRequest)).\nCall `addFence()` for each fence to add.\n\nThe following are required to register, and unregister, a fence:\n\n- A [Google Play Services API Client](/awareness/android-api/get-started#import_awareness_api_dependencies) instance.\n- An `AwarenessFence` instance, which is the fence itself.\n- A `PendingIntent` to handle state changes.\n- A fence key, which is a string that identifies the fence and maps to an `AwarenessFence`-`PendingIntent` pair.\n\nThe following code example shows a method that calls `updateFences()` to\nregister a fence: \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\nTo create multiple fences with unique fence keys, call `addFence()`\nmultiple times. You can use as many `PendingIntent` methods as you need to, but it's\npreferable to use a single `PendingIntent` for all fence callbacks. If you\nuse a fence key that has already been registered to call `addFence()`, the\n`AwarenessFence` and `PendingIntent` values are overwritten for that key.\n\nUnregister a fence\n------------------\n\nTo unregister a fence, call\n[`getFenceClient().updateFences()`](/android/reference/com/google/android/gms/awareness/Awareness#getFenceClient(android.app.Activity)),\nand use [`FenceUpdateRequest.Builder()`](/android/reference/com/google/android/gms/awareness/fence/FenceUpdateRequest.Builder)\nto construct a fence update request. Then call `removeFence()`, as the\nfollowing example shows: \n\n Awareness.getFenceClient(this).updateFences(new FenceUpdateRequest.Builder()\n .removeFence(FENCE_KEY)\n .build())\n .addOnSuccessListener(new OnSuccessListener\u003cVoid\u003e() {\n @Override\n public void onSuccess(Void aVoid) {\n Log.i(TAG, \"Fence was successfully unregistered.\");\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n Log.e(TAG, \"Fence could not be unregistered: \" + e);\n }\n });\n\n| **Note:** You can add and remove fences in the same `FenceUpdateRequest`. Additions and removals are performed in the same order that `addFence()` and `removeFence()` are called in the request.\n\nNext step: [Manage fence callbacks](/awareness/android-api/fence-callbacks)."]]