gpt4 book ai didi

android - PARTIAL_WAKE_LOCK 不起作用

转载 作者:行者123 更新时间:2023-12-04 17:58:22 25 4
gpt4 key购买 nike

我创建了一个目标 API 15 (ICS) 和空 Activity 的新项目。我添加了 list 权限:

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

我在 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);
}

但是设备没有任何反应。根据PowerManager documentation我预计屏幕会(立即)关闭。那么,我是做错了什么还是这不起作用?


编辑:

我尝试了 Ashish Ranjan 的建议,在获取 WakeLock 后手动设置 screenBrightness,但这也不起作用。

最佳答案

根据 Android documentation ,使用 PARTIAL_WAKE_LOCK 不会关闭屏幕。但当此模式在 WakeLock 中处于 Activity 状态时,它会允许屏幕关闭。

因此,设备屏幕不会立即关闭,您必须等待屏幕超时(这取决于设备显示设置中设置的时间)但 CPU 会继续奔跑。

PARTIAL_WAKE_LOCK

Wake lock level: Ensures that the CPU is running; 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.

要关闭屏幕,您必须像这样更改 Window LayoutParams:

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

关于android - PARTIAL_WAKE_LOCK 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38397241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com