همانطور که در صفحه بهترین روشهای حفظ حریم خصوصی توضیح داده شد، برنامهها فقط زمانی باید مجوز BODY_SENSORS_BACKGROUND را بخواهند که برای ویژگی مواجهه با کاربر حیاتی است، و آنها باید این را به درستی برای کاربران افشا کنند.
روند اعطای مجوز به نسخه SDK هدف برنامه شما بستگی دارد.
برنامه اندروید 13 یا بالاتر را هدف قرار می دهد
علاوه بر مجوز BODY_SENSORS موجود، مجوز BODY_SENSORS_BACKGROUND را در فایل مانیفست خود اعلام کنید:
سپس، برنامه شما باید مجوزها را در عملیات جداگانه درخواست کند :
بررسی کنید آیا BODY_SENSORS اعطا شده است. اگر نه، اجازه درخواست کنید.
بررسی کنید آیا BODY_SENSORS_BACKGROUND داده شده است یا خیر. اگر نه، اجازه درخواست کنید.
شکل 1. تنظیمات حسگرها شامل گزینهای به نام تمام زمان است که به دادههای حسگر پسزمینه دسترسی میدهد.
احتیاط: اگر برنامه شما هر دو مجوز حسگر بدن را به طور همزمان درخواست کند، سیستم درخواست را نادیده می گیرد و به برنامه شما اجازه نمی دهد.
در اندروید 13 (سطح API 33) و بالاتر، گفتگوی مجوز زمان اجرا شامل گزینه "همیشه مجاز باشد" نیست. در عوض، کاربران باید دسترسی همیشگی حسگر پسزمینه را از تنظیمات سیستم فعال کنند، همانطور که در شکل 1 نشان داده شده است. وقتی پس از اعطای مجوز BODY_SENSORS ، مجوز BODY_SENSORS_BACKGROUND را درخواست میکنید، میتوانید به کاربران کمک کنید تا به این صفحه تنظیمات حرکت کنند. اگر کاربران دسترسی همیشگی را رد کنند، باید بتوانند به استفاده از برنامه شما ادامه دهند.
برنامه نسخه قبلی را هدف قرار می دهد
شکل 2. گفتگوی مجوز شامل پیوندی برای هدایت کاربران به مجوزهای حسگر برنامه در تنظیمات سیستم است.
وقتی برنامه شما نسخهای از Android را زودتر از Android 13 هدف قرار میدهد، وقتی مجوز BODY_SENSORS را درخواست میکنید، دسترسی پسزمینه بهطور خودکار اعطا نمیشود. در عوض، کاربران گفتگوی سیستمی را مشاهده میکنند که از کاربران دعوت میکند تا به تنظیمات مجوز حسگر برنامه شما بروید، همانطور که در شکل 2 نشان داده شده است. سپس، کاربران باید استفاده از حسگر پسزمینه را در آن صفحه تنظیمات فعال کنند.
کاربران می توانند دسترسی پس زمینه را رد کنند. این همان اثری است که مجوز BODY_SENSORS را در زمانی که برنامه شما در پسزمینه اجرا میکند لغو میکند. وقتی یک برنامه از PassiveMonitoringClient بدون مجوز دسترسی پسزمینه استفاده میکند و به پسزمینه میرود، برنامه مجوز BODY_SENSORS را از دست میدهد و پاسخ تماس onPermissionLost() فراخوانی میشود. به این دلایل، به ویژه مهم است که بهترین شیوه ها را برای درخواست مجوزهای زمان اجرا دنبال کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Request background access to body sensor data\n\nAndroid 13 and Wear OS 4 introduce a way for apps to access body sensors, such\nas heart rate, from the background. This new access model is similar to the one\nthat introduced [background location access in Android 10 (API level 29)](/training/location/permissions#request-background-location).\n\nIf your app needs to access body sensor information in background, such as when\n[monitoring Health Services data in the background](/health-and-fitness/guides/health-services/monitor-background), you must request the\n[`BODY_SENSORS_BACKGROUND`](/reference/android/Manifest.permission#BODY_SENSORS_BACKGROUND) permission.\n| **Note:** `BODY_SENSORS_BACKGROUND` is a restricted permission which cannot be held by your app until the installer adds your app to an allowlist, or until the user lets your app have the permission.\n\nAs described on the [privacy best practices](/privacy/best-practices) page, apps should only ask for\nthe `BODY_SENSORS_BACKGROUND` permission when it is critical to the user-facing\nfeature, and they should properly disclose this to users.\n\nThe process for granting the permission depends on your app's target SDK\nversion.\n\nApp targets Android 13 or higher\n--------------------------------\n\nIn addition to the existing [`BODY_SENSORS`](/reference/android/Manifest.permission#BODY_SENSORS) permission, declare the\n`BODY_SENSORS_BACKGROUND` permission in your manifest file: \n\n \u003cuses-permission android:name=\"android.permission.BODY_SENSORS\"\u003e\n \u003cuses-permission android:name=\"android.permission.BODY_SENSORS_BACKGROUND\"\u003e\n\nThen, your app must [request](/training/permissions/requesting) the permissions in separate operations:\n\n1. Check if `BODY_SENSORS` is granted. If not, request the permission.\n2. Check if `BODY_SENSORS_BACKGROUND` is granted. If not, request the permission.\n\n**Figure 1.** Sensors setting includes an option called **All the\ntime**, which grants background sensor data access. \n**Caution:** If your app requests both body sensor permissions at the\nsame time, the system ignores the request and doesn't grant your app either\npermission.\n\nOn Android 13 (API level 33) and higher, the runtime permission dialog doesn't\ninclude the \"Allow all the time\" option. Instead, users must enable all-the-time\nbackground sensor access from system settings, as shown in figure 1. When you\nrequest the `BODY_SENSORS_BACKGROUND` permission after granting the\n`BODY_SENSORS` permission, you can help users navigate to this settings page. If\nusers decline all-the-time access, they should be able to continue using your\napp.\n\n\u003cbr /\u003e\n\nApp targets an earlier version\n------------------------------\n\n**Figure 2.** Permission dialog includes a link to navigate users to the app's sensor permissions in system settings.\n\nWhen your app targets a version of Android earlier than Android 13, background\naccess isn't granted automatically when you request the `BODY_SENSORS`\npermission. Instead, users see a system dialog that invites users to navigate to\nyour app's sensor permission settings, as shown in figure 2. Then, users must\nenable background sensor usage on that settings page.\n\nUsers can decline the background access. It has the same effect as revoking the\n`BODY_SENSORS` permission while your app is running in the background. When an\napp is using [`PassiveMonitoringClient`](/reference/kotlin/androidx/health/services/client/PassiveMonitoringClient) without background access permission\nand goes into the background, the app loses the `BODY_SENSORS` permission, and\nthe [`onPermissionLost()`](/reference/kotlin/androidx/health/services/client/PassiveListenerService#onPermissionLost()) callback is called. For these reasons, it's\nespecially important that you follow best practices for requesting runtime\npermissions."]]