Android 唤醒屏幕的方式:屏幕锁 - WakeLock / FLAG_KEEP_SCREEN_ON

68 篇文章 1 订阅

Android 屏幕锁 - WakeLock

PowerManager

android.os.PowerManager PowerManager 是用来管理设备的电源状态的类。下面是官方文档

This class gives you control of the power state of the device.

Device battery life will be significantly affected by the use of this API. Do not acquire WakeLocks unless you really need them, use the minimum levels possible, and be sure to release it as soon as you can.

You can obtain an instance of this class by calling Context.getSystemService().

The primary API you'll use is newWakeLock(). This will create a PowerManager.WakeLock object. You can then use methods on this object to control the power state of the device.

        Wake Lock 是一种锁的机制,只要有人拿着这个锁,系统就无法进入休眠,可以被用户态程序和内核获得。这个锁可以是有超时的或者是没有超时的,超时的锁会在时间过去以后自动解锁。如果没有锁了或者超时了, 内核就会启动休眠的那套机制来进入休眠。

        PowerManager.WakeLock 有加锁和解锁两种状态,加锁的方式有两种,一种是永久的锁住,这样的锁除非显式的放开,是不会解锁的,所以这种锁用起来要非常的小心。第二种锁是超时锁,这种锁会在锁住后一段时间解锁。

        在创建了 PowerManager.WakeLock 后,有两种机制,第一种是不计数锁机制,另一种是计数锁机制。可以通过PowerManager.WakeLock.setReferenceCounted(boolean value) 来指定,一般默认为计数机制。这两种机制的区别在于,前者无论 acquire() 了多少次,只要通过一次 release()即可解锁。而后者正真解锁是在( --count == 0 )的时候,同样当 (count == 0) 的时候才会去申请加锁,其他情况 isHeld 状态是不会改变的。所以 PowerManager.WakeLock 的计数机制并不是正真意义上的对每次请求进行申请/释放每一把锁,它只是对同一把锁被申请/释放的次数进行了统计再正真意义上的去操作。

获取锁的方式

public PowerManager.WakeLock newWakeLock (int flags, String tag)

Flags

Android 总共定义了四个 Flags , 你只需要其中的一个,现实上现在只有 PARTIAL_WAKE_LOCK 没有过期,其他的可以使用 FLAG_KEEP_SCREEN_ON 代替。

  • PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。(息屏状态下,不点亮屏幕)
  • SCREEN_DIM_WAKE_LOCK:已过期(API 17)保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯(息屏状态下,点亮屏幕)
  • SCREEN_BRIGHT_WAKE_LOCK:已过期(API 13)保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯(息屏状态下,点亮屏幕)
  • FULL_WAKE_LOCK:已过期(API 17)保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度(息屏状态下,点亮屏幕)
  • ACQUIRE_CAUSES_WAKEUP:正常唤醒锁实际上并不打开照明,这种锁主要针对一些必须通知用户的操作
  • ON_AFTER_RELEASE:当wakelock锁被释放时,计时器会被重置,保持屏幕亮起一段时间,际上并不打开照明

如果使用的是 PARTIAL_WAKE_LOCK 这个 Flag,,那么下面有另外两个 Flag 可以和它一起使用。

  • ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.(翻译:当获取WakeLock时,此标志将强制屏幕和/或键盘立即打开。一个典型的用途是用于通知,这些通知对于用户立即看到很重要。)

  • ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.(如果设置了此标志,则当释放唤醒锁定时,用户活动计时器将重置,从而使照明保持稍长时间。表示执行 release方法后继续保持屏幕亮,直到默认超时后黑屏。)

这个文档下面有个说明:If using this to keep the screen on, you should strongly consider using FLAG_KEEP_SCREEN_ON instead. This window flag will be correctly managed by the platform as the user moves between applications and doesn't require a special permission.还是推荐使用FLAG_KEEP_SCREEN_ON

权限获取

android.permission.WAKE_LOCK 的权限是必须的。

Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an element of the application's manifest.

另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的

Android 屏幕锁 - FLAG_KEEP_SCREEN_ON

官方文档中提示使用 FLAG_KEEP_SCREEN_ON 替换 WakeLock 。

FLAG_KEEP_SCREEN_ON 的官方说明:

Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright. 保持屏幕常亮。

对 Flag 的操作主要有:

getWindow().setFlags(int flags, int mask);
getWindow().addFlags(int flags);
getWindow().clearFlags(int flags);

可以使用 addFlags 和 clearFlags 的方式,目前不清楚这种方式有没有什么不好的地方,在需要的地方对这个 Flag 进行操作,能把屏幕的开关交给系统去处理,在视频播放暂停的时候,释放屏幕。

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

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

参考:

 Android 屏幕锁 - WakeLock | Binkery 技术博客

 Android 屏幕锁 - FLAG_KEEP_SCREEN_ON | Binkery 技术博客

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值