我正在制作一个将在设备锁定时运行的应用程序,通过 Activity#setShowWhenLocked(true)
。我不想阻止设备进入低功耗状态。我知道系统使用 Always-On Display
执行此操作,并且显示器为此具有关联的电源模式:
/frameworks/base/core/java/android/view/Display.java
286 /**
287 * Display state: The display is dozing in a low power state; it is still
288 * on but is optimized for showing system-provided content while the
289 * device is non-interactive.
290 *
291 * @see #getState
292 * @see android.os.PowerManager#isInteractive
293 */
294 public static final int STATE_DOZE = ViewProtoEnums.DISPLAY_STATE_DOZE; // 3
295
296 /**
297 * Display state: The display is dozing in a suspended low power state; it is still
298 * on but the CPU is not updating it. This may be used in one of two ways: to show
299 * static system-provided content while the device is non-interactive, or to allow
300 * a "Sidekick" compute resource to update the display. For this reason, the
301 * CPU must not control the display in this mode.
302 *
303 * @see #getState
304 * @see android.os.PowerManager#isInteractive
305 */
306 public static final int STATE_DOZE_SUSPEND = ViewProtoEnums.DISPLAY_STATE_DOZE_SUSPEND; // 4
/frameworks/base/core/java/android/os/PowerManagerInternal.java中也有这一段:
51 * Wakefulness: The device is dozing. It is almost asleep but is allowing a special
52 * low-power "doze" dream to run which keeps the display on but lets the application
53 * processor be suspended. It can be awoken by a call to wakeUp() which ends the dream.
54 * The device fully goes to sleep if the dream cannot be started or ends on its own.
55 */
56 public static final int WAKEFULNESS_DOZING = 3;
如果可能的话,我想避免使用 root 来执行此操作,但如果所有其他方法都失败了,我可以 manually control doze with root using these commands ,但这听起来很困惑,我不想弄乱任何其他应用程序的打瞌睡交互。
此外,我只特别关注 Android Pie (9.0) 及更高版本。
更新:
我也尝试过获取DOZE_WAKE_LOCK
,但它需要系统权限DEVICE_POWER
。我试图adb shell pm grant
我的应用程序的权限,但它不是可更改的权限类型。
最佳答案
你可以通过 DreamService 来做到这一点这是一个屏幕保护程序。您不能通过 Activity 执行此操作。 Activity 阻止设备进入休眠模式。
Dreams are interactive screensavers launched when a charging device is idle, or docked in a desk dock. Dreams provide another modality for apps to express themselves, tailored for an exhibition/lean-back experience.
关于android - 当我的应用程序打开并且屏幕亮着时,如何允许 Android 打瞌睡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990019/