یک حصار ایجاد کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
حصار یک یا چند شرایط زمینه را تعریف می کند که برنامه شما می تواند به آن واکنش نشان دهد. هنگامی که وضعیت حصار تغییر می کند، برنامه شما یک تماس پاسخ دریافت می کند.
دو نوع حصار وجود دارد: حصارهای اولیه که مجموعه اولیه سیگنال های زمینه را نشان می دهند و حصارهای ترکیبی که چندین حصار اولیه را با استفاده از عملگرهای بولی ترکیب می کنند. همه نرده ها نمونه هایی از AwarenessFence
هستند.
یک حصار بدوی ایجاد کنید
حصارهای اولیه، که مجموعه اصلی سیگنال های زمینه را نشان می دهند، در بسته awareness.fence
تعریف شده اند. مثال زیر ایجاد یک حصار ساده را نشان میدهد که وقتی فعالیت شناساییشده کاربر WALKING
باشد TRUE
است و در غیر این صورت FALSE
:
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
در مثال قبل، DetectedActivityFence
با فراخوانی در during
ایجاد شد، به این معنی که هر زمان که کاربر در WALKING
است، حصار در حالت TRUE
قرار دارد.
به انتقال ها واکنش نشان دهید
هر نوع حصار ابتدایی، به استثنای TimeFence
، همچنین میتواند بهطور لحظهای هنگام انتقال حالت زمینه فعال شود. برای مثال، میتوانید یک DetectedActivityFence
تنظیم کنید تا زمانی که کاربر starting
یا stopping
فعالیت است، بهطور لحظهای فعال شود. حصارهای انتقالی برای چند ثانیه قبل از اینکه دوباره FALSE
شوند در حالت TRUE
قرار دارند.
یک حصار ترکیبی ایجاد کنید
نرده های ترکیبی چندین نوع حصار ابتدایی را با استفاده از عملگرهای بولی ترکیب می کنند. مثال زیر ایجاد یک حصار ترکیبی را نشان می دهد که هنگام راه رفتن کاربر و اتصال هدفون به برق فعال می شود:
// 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 متر از مکان فعلی حرکت می کند، یا بیش از یک ساعت از زمان فعلی گذشته است، فعال می شود.
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));
مرحله بعدی: ثبت حصار .
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\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)."]]