API مناسب را برای بیدار نگه داشتن دستگاه انتخاب کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
هنگامی که کاربر دستگاه مجهز به اندروید خود را بیکار می گذارد، برای جلوگیری از تخلیه باتری، به سرعت به حالت تعلیق می رود. با این حال، مواقعی وجود دارد که یک برنامه باید از رفتن CPU به حالت تعلیق جلوگیری کند. در برخی موارد، برنامه ممکن است نیاز داشته باشد هنگام کار کردن، صفحه نمایش را روشن نگه دارد. در موارد دیگر، برنامه نیازی به روشن نگه داشتن صفحه نمایش ندارد، اما همچنان به CPU برای فعال بودن نیاز دارد.
رویکرد شما بستگی به نیازهای برنامه شما دارد. با این حال، یک قانون کلی این است که شما باید از سبک ترین رویکرد ممکن استفاده کنید تا تاثیر برنامه خود را بر منابع سیستم به حداقل برسانید. این سند به شما کمک می کند تا فناوری اندروید مناسب را برای شرایط خود انتخاب کنید.
تکنولوژی مناسب را انتخاب کنید
بهترین گزینه برای بیدار نگه داشتن دستگاه شما به نیازهای برنامه شما بستگی دارد. این بخش به شما کمک می کند تا روش مناسب را انتخاب کنید.
آیا برنامه شما نیاز به روشن نگه داشتن صفحه نمایش دارد؟
اگر بله ، روشن نگه داشتن صفحه را ببینید. ممکن است یک API با هدف خاص وجود داشته باشد که آنچه شما نیاز دارید را انجام دهد. به عنوان مثال، اگر از رابط کاربری تماس تلفنی استفاده میکنید، میتوانید از چارچوب Android Telecom استفاده کنید که در صورت لزوم صفحه را روشن نگه میدارد. اگر API با هدف خاصی برای موقعیت شما وجود ندارد، می توانید از API keepScreenOn استفاده کنید.
آیا برنامه شما یک سرویس پیشزمینه اجرا میکند و باید دستگاه را هنگامی که صفحه نمایش خاموش است در حین اجرای سرویس، بیدار نگه دارید؟
اگر خیر ، نیازی نیست دستگاه را بیدار نگه دارید. اگر کاربر به طور فعال با برنامه تعامل داشته باشد، دستگاه بیدار خواهد ماند. اگر کاربر با برنامه شما تعاملی ندارد و شما سرویس پیش زمینه را اجرا نمی کنید، باید در صورت لزوم به دستگاه اجازه دهید به حالت تعلیق برود. اگر فقط باید مطمئن شوید زمانی که کاربر از برنامه دور است برخی از کارها انجام می شود، برای یافتن بهترین گزینه، به مستندات کارهای پس زمینه مراجعه کنید.
اگر بله ، ابتدا تأیید کنید که واقعاً باید از یک سرویس پیش زمینه استفاده کنید. بسته به موقعیت شما، ممکن است برخی از API های با هدف خاص وجود داشته باشد که می توانید به جای یک سرویس پیش زمینه، از آن برای برآورده کردن نیاز خود استفاده کنید. می توانید اطلاعات مربوط به این موارد را در اسناد خدمات پیش زمینه بیابید. به عنوان مثال، اگر نیاز به ردیابی مکان کاربر دارید، ممکن است بتوانید از API geofencing به جای سرویس پیش زمینه location استفاده کنید.
آیا اگر دستگاه در حین اجرا شدن سرویس پیش زمینه و خاموش بودن صفحه نمایش دستگاه به حالت تعلیق درآید، برای تجربه کاربر مضر خواهد بود؟ (به عنوان مثال، اگر از یک سرویس پیش زمینه برای به روز رسانی اعلان ها استفاده می کنید، اگر دستگاه به حالت تعلیق درآمده باشد، تجربه کاربری بدی نخواهد بود.)
اگر خیر ، از wakelock استفاده نکنید. زمانی که کاربر با دستگاه خود درگیر شد، این عمل بهطور خودکار از سر گرفته میشود، که آن را از حالت تعلیق خارج میکند.
اگر بله ، ممکن است لازم باشد از یک قفل بیدار استفاده کنید . با این حال، همچنان باید بررسی کنید که آیا در حال حاضر از یک API استفاده میکنید یا عملی را انجام میدهید که یک wake lock را از طرف شما انجام میدهد، همانطور که در اقداماتی که دستگاه را بیدار نگه میدارد بحث شده است.
اقداماتی که دستگاه را بیدار نگه می دارد
اگر برنامه شما یکی از کارهای زیر را انجام میدهد، نیازی نیست خودتان یک قفل بیپروا تنظیم کنید. اقدامات و APIهای زیر همگی دستگاه را برای شما بیدار نگه می دارند.
اگر در حال پخش صدا هستید، سیستم صوتی یک wake lock را برای شما تنظیم و مدیریت می کند. نیازی نیست خودتان این کار را انجام دهید
اگر از APIهای زمانبندی کار یا کتابخانههایی مانند WorkManager ، JobScheduler ، یا DownloadManager استفاده میکنید، سیستم یا کتابخانه یک wake lock دریافت میکند که به برنامه شما نسبت داده میشود.
سنسورهای دستگاه خاصی سنسورهای بیدار شدن هستند. میتوانید از SensorManager استفاده کنید تا این حسگرها وقتی دادههایی برای گزارش دارند، دستگاه را بیدار کنند. برای بررسی اینکه آیا یک سنسور یک حسگر بیداری است یا خیر، با Sensor.isWakeUpSensor تماس بگیرید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Choose the right API to keep the device awake\n\nWhen the user leaves their Android-powered device idle, it quickly goes into the\nsuspend state to avoid draining the battery. However, there are times when an\napp needs to prevent the CPU from going to the suspend state. In some cases, the\napp may need to keep the screen on while it's working. In other cases, the app\ndoesn't need to keep the screen on but still needs the CPU to be active.\n\nThe approach you take depends on the needs of your app. However, a general rule\nis that you should use the most lightweight approach possible, to minimize your\napp's impact on system resources. This document helps you choose the correct\nAndroid technology for your situation.\n| **Note:** You may be familiar with **wake locks**. An app can set a wake lock to keep the device from suspending. However, using a wake lock can quickly drain the device battery. You should only use a wake lock if there's no other option that will do what you need. If you do use a wake lock, you should release it as soon as possible.\n\nChoose the right technology\n---------------------------\n\nThe best option for keeping your device awake depends on your app's needs. This\nsection helps you choose the right approach.\n\n- Does your app need to keep the screen on?\n - If **Yes** , see [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on). There may be a special-purpose API that does what you need; for example, if you're implementing a phone-call UI, you can use the [Android telecom\n framework](/reference/android/telecom/package-summary), which keeps the screen on when needed. If there's no special purpose API for your situation, you can use the `keepScreenOn` API.\n- Is your app running a foreground service, and you need to keep the device awake when screen is off while the service is running?\n - If **No** , you do not need to keep the device awake. If the user is actively interacting with the app, the device will stay awake. If the user is not interacting with your app and you are not running a foreground service, you should let the device go into suspend mode when necessary. If you just need to make sure some work gets done while the user is away from the app, see the [background tasks](/develop/background-work/background-tasks) documentation to find the best option.\n - If **Yes** , first confirm that you actually need to use a foreground service. Depending on your situation, there may be some special-purpose API you can use to accomplish your need instead of a foreground service. You can find information about these [in the Foreground Service\n documentation](/develop/background-work/services/fgs/service-types). For example, if you need to track the user's location, you might be able to use the [geofencing API](/develop/sensors-and-location/location/geofencing) instead of a `location` foreground service.\n- Would it be detrimental to the user experience if the device suspends while the foreground service is running and the device screen is off? (For example, if you're using a foreground service to update notifications, it wouldn't be a bad user experience if the device is suspended.)\n - If **No**, do not use a wakelock. The action resumes automatically once the user engages with their device, which takes it out of suspend.\n - If **Yes** , you might need to [use a wake lock](/develop/background-work/background-tasks/awake/wakelock). However, you should still check to see if you're already using an API or doing an action that declares a wake lock on your behalf, as discussed in [Actions that keep the device awake](#actions-keep).\n\nActions that keep the device awake\n----------------------------------\n\nIf your app is doing any of the following, you don't need to set a wake lock\nyourself. The following actions and APIs all keep the device awake for you.\n\n- If you're playing audio, the audio system sets and manages a wake lock for you; you don't need to do it yourself.\n- If you're using task scheduling APIs or libraries such as [WorkManager](/develop/background-work/background-tasks/persistent), [`JobScheduler`](/reference/android/app/job/JobScheduler), or [`DownloadManager`](/reference/android/app/DownloadManager), the system or library acquires a wake lock that is attributed to your app.\n- If you're using [Media3 ExoPlayer](/media/media3/exoplayer), you can use [`ExoPlayer.setWakeMode()`](/reference/androidx/media3/exoplayer/ExoPlayer#setWakeMode(int)) to have the player set a wake lock for you.\n- Certain device sensors are wake-up sensors; you can use [`SensorManager`](/reference/android/hardware/SensorManager) to have those sensors wake up the device when they have data to report. To check if a sensor is a wake-up sensor, call [`Sensor.isWakeUpSensor`](/reference/android/hardware/Sensor#isWakeUpSensor())).\n\nSee also\n--------\n\n- [Use wake locks](/develop/background-work/background-tasks/awake/wakelock)\n- [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on)"]]