با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این صفحه در مورد موارد زیر بحث می کند:
نحوه درخواست دسترسی به موقعیت مکانی پس زمینه
نحوه رسیدگی به درخواست بر اساس نسخه SDK هدف برنامه شما.
اینکه چگونه تنظیمات برگزیده کاربر برای مکان تقریبی بر نحوه دریافت مکان پسزمینه برنامه شما تأثیر میگذارد.
شکل 7. صفحه تنظیمات شامل گزینه ای به نام Allow all time است که به موقعیت مکانی پس زمینه دسترسی می دهد.
محتویات گفتگوی مجوز به نسخه SDK هدف بستگی دارد
هنگامی که یک ویژگی در برنامه شما مکان پسزمینه را در دستگاهی که Android 10 اجرا میکند (سطح API 29) درخواست میکند، کادر گفتگوی مجوزهای سیستم شامل گزینهای به نام Allow all time است. اگر کاربر این گزینه را انتخاب کند، ویژگی در برنامه شما به موقعیت مکانی پسزمینه دسترسی پیدا میکند.
با این حال، در اندروید 11 (سطح API 30) و بالاتر، کادر گفتگوی سیستم شامل گزینه Allow all time نیست. در عوض، کاربران باید موقعیت پسزمینه را در صفحه تنظیمات فعال کنند، همانطور که در شکل 7 نشان داده شده است.
میتوانید با پیروی از بهترین روشها هنگام درخواست مجوز موقعیت مکانی پسزمینه، به کاربران کمک کنید به این صفحه تنظیمات حرکت کنند. روند اعطای مجوز به نسخه SDK هدف برنامه شما بستگی دارد.
برنامه اندروید 11 یا بالاتر را هدف قرار می دهد
اگر به برنامه شما مجوز ACCESS_BACKGROUND_LOCATION اعطا نشده است و shouldShowRequestPermissionRationale()true را برگرداند، یک رابط کاربری آموزشی به کاربران نشان دهید که شامل موارد زیر است:
توضیح واضح در مورد اینکه چرا ویژگی برنامه شما نیاز به دسترسی به موقعیت مکانی پس زمینه دارد.
برچسب قابل مشاهده توسط کاربر گزینه تنظیمات که موقعیت پسزمینه را اعطا میکند (به عنوان مثال، Allow all time در شکل 7). برای دریافت این برچسب می توانید getBackgroundPermissionOptionLabel() فراخوانی کنید. مقدار بازگشتی این روش به ترجیح زبان دستگاه کاربر محلی سازی شده است.
گزینه ای برای کاربران برای رد کردن مجوز. اگر کاربران دسترسی به موقعیت مکانی پسزمینه را رد کنند، باید بتوانند به استفاده از برنامه شما ادامه دهند.
شکل 8. اعلانی که به کاربر یادآوری می کند که به یک برنامه دسترسی به موقعیت مکانی پس زمینه داده است.
برنامه اندروید 10 یا پایینتر را هدف قرار میدهد
وقتی یک ویژگی در برنامه شما درخواست دسترسی به موقعیت مکانی پسزمینه را میدهد، کاربران یک گفتگوی سیستم را میبینند. این گفتگو شامل گزینه ای برای پیمایش به گزینه های مجوز مکان برنامه شما در صفحه تنظیمات است.
تا زمانی که برنامه شما در حال حاضر از بهترین شیوه ها برای درخواست مجوزهای مکان پیروی می کند، نیازی به ایجاد هیچ تغییری برای پشتیبانی از این رفتار ندارید.
کاربر می تواند بر دقت موقعیت مکانی پس زمینه تأثیر بگذارد
اگر کاربر مکان تقریبی را درخواست کند ، انتخابهای کاربر در گفتگوی مجوزهای مکان برای مکان پسزمینه نیز اعمال میشود. به عبارت دیگر، اگر کاربر مجوز ACCESS_BACKGROUND_LOCATION را به برنامه شما اعطا کند اما فقط به موقعیت مکانی تقریبی در پیش زمینه دسترسی داشته باشد، برنامه شما فقط به موقعیت مکانی تقریبی در پس زمینه نیز دسترسی دارد.
منابع اضافی
برای اطلاعات بیشتر در مورد مجوزهای مکان در Android، مطالب زیر را مشاهده کنید:
نمونه برنامه برای نشان دادن استفاده از مجوزهای مکان.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],null,["This page discusses the following:\n\n- How to request background location access.\n- How to handle the request based on your app's target SDK version.\n- How user preferences for approximate location affect how your app gets background location.\n\n**Note:** If a feature in your app accesses location from the background, verify that access is necessary. Consider getting the information that the feature needs in other ways. To learn more about background location access, see the [Access location in the background](/training/location/background) page. \n**Figure 7.** Settings page includes an option called **Allow all the time**, which grants background location access.\n\nPermission dialog contents depend on target SDK version\n\nWhen a feature in your app requests background location on a device that runs\nAndroid 10 (API level 29), the system permissions dialog includes an option\nnamed **Allow all the time**. If the user selects this option, the feature in\nyour app gains background location access.\n\nOn Android 11 (API level 30) and higher, however, the system dialog doesn't\ninclude the **Allow all the time** option. Instead, users must enable background\nlocation on a settings page, as shown in figure 7.\n\nYou can help users navigate to this settings page by following best practices\nwhen requesting the background location permission. The process for granting the\npermission depends on your app's target SDK version.\n\nApp targets Android 11 or higher\n\nIf your app hasn't been granted the `ACCESS_BACKGROUND_LOCATION` permission, and\n[`shouldShowRequestPermissionRationale()`](/reference/androidx/core/app/ActivityCompat#shouldShowRequestPermissionRationale(android.app.Activity,%20java.lang.String)) returns `true`, show an\neducational UI to users that includes the following:\n\n- A clear explanation of why your app's feature needs access to background location.\n- The user-visible label of the settings option that grants background location (for example, **Allow all the time** in figure 7). You can call [`getBackgroundPermissionOptionLabel()`](/reference/android/content/pm/PackageManager#getBackgroundPermissionOptionLabel()) to get this label. The return value of this method is localized to the user's device language preference.\n- An option for users to decline the permission. If users decline background location access, they should be able to continue using your app.\n\n**Figure 8.**Notification reminding the user that they've granted background location access to an app.\n\nApp targets Android 10 or lower\n\nWhen a feature in your app requests background location access, users see a\nsystem dialog. This dialog includes an option to navigate to your app's location\npermission options on a settings page.\n\nAs long as your app already follows best practices for requesting location\npermissions, you don't need to make any changes to support this behavior.\n\nUser can affect background location accuracy\n\nIf the [user requests approximate location](/develop/sensors-and-location/location/permissions/runtime#approximate-request), the user's choices in the\nlocation permissions dialog also apply to background location. In other words,\nif the user grants your app the `ACCESS_BACKGROUND_LOCATION` permission but\ngrants only approximate location access in the foreground, your app has only\napproximate location access in the background as well.\n\nAdditional resources\n\nFor more information about location permissions in Android, view the following\nmaterials:\n\nCodelabs\n\n- [Privacy best practices](/codelabs/android-privacy-codelab)\n\nVideos\n\n- [How to find possible background location usage](https://www.youtube.com/watch?v=xTVeFJZQ28c)\n\nSamples\n\n- [Sample app](https://github.com/android/platform-samples/tree/main/samples/location/src/main/java/com/example/platform/location/permission) to demonstrate the use of location permissions."]]