Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I allow Android to Doze while my app is open and the screen is on?

I am making an app that will run when the device is locked, via Activity#setShowWhenLocked(true). I do not want to prevent the device from entering a low-power state. I know that the system does this with Always-On Display, and the display has an associated power mode for that:

/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

There is also this section in /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;

If possible, I would like to avoid using root to do this, but if all else fails I could manually control doze with root using these commands, but that sounds messy and I don't want to mess up any other app's dozing interactions.

Also, I'm only particularly concerned with Android Pie (9.0) and later.

Update:

I have also tried acquiring a DOZE_WAKE_LOCK, but it requires the system permission DEVICE_POWER. I tried to adb shell pm grant the permission to my app, but it is not a changeable permission type.

like image 916
BLuFeNiX Avatar asked Oct 25 '18 13:10

BLuFeNiX


People also ask

How do I turn on doze mode on Android?

If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services.

How do I keep my Android screen active?

Open the Settings app on your phone. Tap on Home screen, Lock screen & Always-On Display. Select Always-On Display. Choose from one of the default options or tap "+" to customize your own.

How do I keep my Android tablet screen on?

Navigate to Settings and then search for and select Keep screen on while viewing. Tap the switch to turn the feature on. This prevents the display from turning off while you are looking at it.


1 Answers

You could do this by DreamService which is a Screen Saver. You cannot do this by Activity. Activity prevent the device enter sleep mode.

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.

like image 196
Fung LAM Avatar answered Sep 19 '22 12:09

Fung LAM