简体   繁体   English

PARTIAL_WAKE_LOCK不起作用

[英]PARTIAL_WAKE_LOCK does not work

I created a new project with target API 15 (ICS) with empty Activity. 我使用目标API 15(ICS)创建了一个新项目,活动为空。 I added permission to manifest: 我添加了显示权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

I added code to onCreate() : 我向onCreate()添加了代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PowerManager pm = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
        wl.acquire();

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.screenBrightness = 0;
        getWindow().setAttributes(params);
    }

But nothing happens with the device. 但是设备没有任何反应。 According to PowerManager documentation I expected the screen to go off (immediately). 根据PowerManager文档,我希望屏幕立即关闭。 So, am I doing something wrong or is this not working? 那么,我是在做错什么还是不起作用?


EDIT: 编辑:

I tried Ashish Ranjan's suggestion to manually set screenBrightness after acquiring WakeLock but this too does not work. 我在获取WakeLock之后尝试了Ashish Ranjan的建议来手动设置screenBrightness ,但这也行不通。

As per the Android documentation , using PARTIAL_WAKE_LOCK won't turn the screen off. 根据Android 文档 ,使用PARTIAL_WAKE_LOCK不会关闭屏幕。 But it will allow the screen to go off, when this mode is active in the WakeLock . 但是,当WakeLock中的此模式处于活动状态时,它将允许屏幕关闭。

So, the device screen won't turn off instantly, you'll have to wait for the screen to timeout (which depends on the time set in the device display settings) but the CPU will keep running. 因此,设备屏幕不会立即关闭,您必须等待屏幕超时(取决于设备显示设置中设置的时间),但CPU会继续运行。

PARTIAL_WAKE_LOCK PARTIAL_WAKE_LOCK

Wake lock level: Ensures that the CPU is running; 唤醒锁定级别:确保CPU在运行; the screen and keyboard backlight will be allowed to go off. 屏幕和键盘背光将被允许熄灭。

If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released. 如果用户按下电源按钮,则屏幕将关闭,但CPU将保持打开状态,直到所有部分唤醒锁都已释放。

To turn off the screen you'll have to change the Window LayoutParams like this : 要关闭屏幕,您必须像这样更改Window LayoutParams:

WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);

我认为您忘了添加:

 wl.release();

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

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