קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
כשהמשתמש משאיר את המכשיר עם Android ללא פעילות, הוא עובר במהירות למצב השהיה כדי למנוע שחיקה של הסוללה. עם זאת, יש מקרים שבהם אפליקציה צריכה למנוע מהמעבד לעבור למצב השהיה. במקרים מסוימים, יכול להיות שהאפליקציה תצטרך להשאיר את המסך דולק בזמן שהיא פועלת. במקרים אחרים, האפליקציה לא צריכה להשאיר את המסך פועל, אבל עדיין צריכה שהמעבד יהיה פעיל.
הגישה שתבחרו תלויה בצרכים של האפליקציה. עם זאת, ככלל, כדאי להשתמש בגישה הקלה ביותר האפשרית כדי למזער את ההשפעה של האפליקציה על משאבי המערכת. המסמך הזה יעזור לכם לבחור את טכנולוגיית Android המתאימה למצב שלכם.
בחירת הטכנולוגיה המתאימה
האפשרות הטובה ביותר לשמירה על המכשיר במצב פעיל תלויה בצרכים של האפליקציה. בקטע הזה נסביר איך לבחור את הגישה המתאימה.
האם האפליקציה שלכם צריכה להשאיר את המסך דלוק?
אם התשובה היא כן, אפשר לעיין במאמר המסך יישאר דלוק. יכול להיות שיש ממשק API למטרה מיוחדת שיעשה את מה שאתם צריכים. לדוגמה, אם אתם מטמיעים ממשק משתמש לשיחות טלפון, אתם יכולים להשתמש במסגרת התקשורת של Android, שמשאירה את המסך דלוק כשצריך. אם אין ממשק API למטרה ספציפית שמתאים למקרה שלכם, תוכלו להשתמש ב-API keepScreenOn.
האם האפליקציה שלכם מפעילה שירות בחזית, ואתם צריכים לשמור על המכשיר פעיל כשהמסך כבוי בזמן שהשירות פועל?
אם התשובה היא לא, אין צורך להשאיר את המכשיר במצב פעיל. אם המשתמש יהיה באינטראקציה פעילה עם האפליקציה, המכשיר יישאר במצב פעיל. אם המשתמש לא מבצע אינטראקציה עם האפליקציה ואתם לא מריצים שירות בחזית, כדאי לאפשר למכשיר לעבור למצב השהיה במקרה הצורך. אם אתם רק רוצים לוודא שחלק מהעבודה מתבצעת בזמן שהמשתמש לא נמצא באפליקציה, תוכלו לעיין במסמכי העזרה בנושא משימות ברקע כדי למצוא את האפשרות הטובה ביותר.
אם התשובה היא כן, קודם צריך לוודא שאתם באמת צריכים להשתמש בשירות בחזית. בהתאם למצב שלכם, יכול להיות שיש ממשק API למטרה מיוחדת שתוכלו להשתמש בו כדי להשיג את הצורך שלכם במקום בשירות בחזית.
מידע על הנושאים האלה זמין במסמכי העזרה של שירותי חזית. לדוגמה, אם אתם צריכים לעקוב אחרי המיקום של המשתמש, יכול להיות שתוכלו להשתמש ב-Geofencing API במקום בשירות location בחזית.
האם השהיית המכשיר בזמן שהשירות בחזית פועל והמסך כבוי תזיק לחוויית המשתמש? (לדוגמה, אם אתם משתמשים בשירות שפועל בחזית כדי לעדכן התראות, חוויית המשתמש לא תיפגע אם המכשיר יושעה).
אם הערך הוא No, אין להשתמש ב-wakelock. הפעולה תתחדש באופן אוטומטי ברגע שהמשתמש יבצע פעולה במכשיר, וכך יבטל את ההשעיה.
אם האפליקציה מבצעת אחת מהפעולות הבאות, אין צורך להגדיר את נעילת ההתעוררות בעצמכם. הפעולות וממשקי ה-API הבאים שומרים על המכשיר פעיל.
אם אתם מפעילים אודיו, מערכת האודיו מגדירה ומנהלת עבורכם את נעילת ההתעוררות. אתם לא צריכים לעשות זאת בעצמכם.
אם אתם משתמשים בספריות או ב-API לניהול משימות, כמו WorkManager, JobScheduler או DownloadManager, המערכת או הספרייה מקבלות את נעילת ההתעוררות שמשויכת לאפליקציה.
חיישנים מסוימים במכשיר הם חיישני התעוררות. אפשר להשתמש ב-SensorManager כדי לגרום לחיישנים האלה להעיר את המכשיר כשיש להם נתונים לדיווח. כדי לבדוק אם חיישן מסוים הוא חיישן התעוררות, צריך להפעיל את הפונקציה Sensor.isWakeUpSensor.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[[["התוכן קל להבנה","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-26 (שעון UTC)."],[],[],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)"]]