唤醒锁和 FLAG_KEEP_SCREEN_ON 之间的区别?

发布于 2024-10-06 14:05:20 字数 348 浏览 0 评论 0原文

保持屏幕唤醒可以通过使用唤醒锁来完成

mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
                getClass().getName());

,或者通过将 FLAG_KEEP_SCREEN_ON 添加到窗口来完成,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

两者之间的技术差异是什么以及在推荐的性能和电池寿命方面是什么?

Keeping the screen awake can be accomplished by using a wakelock by

mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
                getClass().getName());

Or by adding the FLAG_KEEP_SCREEN_ON to the window,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

What is the technical difference between the two and in terms of performance and battery life which is recommended?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟凡古楼 2024-10-13 14:05:20

唤醒锁为您提供了更多控制权(例如唤醒手机以在不关闭屏幕的情况下下载某些内容),并且要求您的应用程序持有唤醒锁权限。

因此,如果您只想在窗口可见时保持屏幕打开,则建议使用FLAG_KEEP_SCREEN_ON

A wakelock gives you way more control (Like waking the phone to download something without turning the screen off) and requires your application to hold the wakelock permission.

Therefore FLAG_KEEP_SCREEN_ON is recommended if all you want is to keep the screen on while your window is visible.

寒江雪… 2024-10-13 14:05:20

Wakelock 是模糊的,因为它有许多不同的选项。标志 FLAG_KEEP_SCREEN_ON 只能做到这一点。

| Flag Value              | CPU | Screen | Keyboard |
-----------------------------------------------------
| PARTIAL_WAKE_LOCK       | On* | Off    | Off      |
| SCREEN_DIM_WAKE_LOCK    | On  | Dim    | Off      |
| SCREEN_BRIGHT_WAKE_LOCK | On  | Bright | Off      |
| FULL_WAKE_LOCK          | On  | Bright | Bright   |

请参阅唤醒锁PowerManager 了解 Android 的具体信息,以及具体实现的其他答案。

Wakelock is vague, since it has many different options. The flag FLAG_KEEP_SCREEN_ON only does that.

| Flag Value              | CPU | Screen | Keyboard |
-----------------------------------------------------
| PARTIAL_WAKE_LOCK       | On* | Off    | Off      |
| SCREEN_DIM_WAKE_LOCK    | On  | Dim    | Off      |
| SCREEN_BRIGHT_WAKE_LOCK | On  | Bright | Off      |
| FULL_WAKE_LOCK          | On  | Bright | Bright   |

Please see wakelock or PowerManager for Android specifics, and other answers for the exact implementation.

若能看破又如何 2024-10-13 14:05:20

唤醒锁用于后台服务,以在屏幕关闭时保持CPU运行以完成工作。你永远不应该使用唤醒
锁定一项活动。要使用唤醒锁,必须在应用程序的清单文件中添加 WAKE_LOCK 权限。


FLAG_KEEP_SCREEN_ON 用于activity中保持屏幕开启,这也会保持CPU开启,无需任何特殊处理
权限,与唤醒锁不同。您永远不应该使用 FLAG_KEEP_SCREEN_ON
在服务中。

干杯!

Wake lock is used in background services to keep the CPU running to do work while the screen is off. You should never use wake
lock in an activity. To use wake lock, WAKE_LOCK permission must be added in application's manifest file.


FLAG_KEEP_SCREEN_ON is used in activity to keep the screen turned on, which will also keep the CPU on without any special
permission, unlike wake lock. You should never use FLAG_KEEP_SCREEN_ON
in a service.

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文