简体   繁体   English

屏幕会使用PowerManager自动唤醒。ON_AFTER_RELEASEakeLock

[英]Screen automatically wakes up with PowerManager.ON_AFTER_RELEASE wakeLock

In the following sample activity, the screen automatically wakes up right after the user switches it off (with power button), but only if I use the PowerManager.ON_AFTER_RELEASE flag. 在以下示例活动中,仅在我使用PowerManager.ON_AFTER_RELEASE标志时,用户关闭屏幕(带有电源按钮)后,屏幕会立即自动唤醒。 It doesn't happen if I don't use this flag. 如果我不使用此标志,则不会发生。

public class TestActivity extends Activity {

    private WakeLock wakeLock;

    @Override
    protected void onResume() {
        super.onResume();

        wakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "test");
        wakeLock.acquire();
    }

    @Override
    protected void onPause() {
        wakeLock.release();
        super.onPause();
    }

}

Is there a way to avoid this behaviour while still using the PowerManager.ON_AFTER_RELEASE flag ? 有没有办法在仍然使用PowerManager.ON_AFTER_RELEASE标志时避免这种现象?

I have the same issue when using the MediaPlayer's setWakeMode() method (I checked in the source code and it also uses this PowerManager.ON_AFTER_RELEASE flag) 使用MediaPlayer的setWakeMode()方法时,我遇到了同样的问题(我在源代码中进行了检查,它也使用了此PowerManager.ON_AFTER_RELEASE标志)

Or maybe I just misunderstand the purpose of this flag...what is it made for then ? 或者,也许我只是误解了该标志的用途……它的用途是什么?

The WindowManager.LayoutParams class has a flag, FLAG_TURN_SCREEN_ON . WindowManager.LayoutParams类具有标志FLAG_TURN_SCREEN_ON It's documented as follows: 记录如下:

Window flag: when set as a window is being added or made visible, once the window has been shown then the system will poke the power manager's user activity (as if the user had woken up the device) to turn the screen on. 窗口标志:添加或设置为可见窗口后,一旦显示了该窗口,系统便会拨开电源管理器的用户活动(就像用户已经唤醒设备一样)以打开屏幕。

Try clearing this flag in your onResume : 尝试在onResume清除此标志:

getWindow().clearFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON );

and see if that makes the screen stay dark after the power button is hit. 并查看是否在按下电源按钮后使屏幕保持黑屏。 You could experiment with the flag in onPause too. 您也可以在onPause尝试使用该标志。 Just a guess. 只是一个猜测。

Ok, after many experiments it seems that there is no way to avoid this auto screen wakeup when using this PowerManager.ON_AFTER_RELEASE . 好的,经过多次实验,使用PowerManager.ON_AFTER_RELEASE似乎无法避免这种自动屏幕唤醒。

As a consequence it's not possible to use MediaPlayer.setWakeMode() without having this issue. 因此,如果没有这个问题,就无法使用MediaPlayer.setWakeMode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM