如何在我的应用程序打开且屏幕打开时允许Android打瞌睡?

BLu*_*NiX 8 android android-source android-doze-and-standby android-doze android-9.0-pie

我正在创建一个应用程序,它将在设备锁定时运行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
Run Code Online (Sandbox Code Playgroud)

/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;
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我想避免使用root来执行此操作,但如果所有其他方法都失败了,我可以使用这些命令手动控制root用户打瞌睡,但这听起来很混乱,我不想搞砸任何其他应用程序的打瞌睡交互.

另外,我只关心Android Pie(9.0)

更新:

我也尝试过获取DOZE_WAKE_LOCK,但它需要系统权限DEVICE_POWER.我尝试adb shell pm grant了对我的应用程序的权限,但它不是可更改的权限类型.

Fun*_*LAM 1

您可以通过DreamService(屏幕保护程序)来执行此操作。您无法通过“活动”来执行此操作。活动阻止设备进入睡眠模式。

Dreams 是在充电设备空闲或插入桌面底座时启动的交互式屏幕保护程序。梦想为应用程序提供了另一种表达自我的方式,专为展览/向后倾斜体验而定制。