androidwakelock_Android Wake Lock机制

androidwakelock_Android Wake Lock机制PowerManager.WakeLockpublicfinalclassPowerManager.WakeLockextendsObjectjava.lang.Object↳android.os.PowerManager.WakeLockAwakelockisamechanismtoindicatethatyourapplicationneedstohavet…

PowerManager.WakeLock

public final class PowerManager.WakeLock

extends Object

java.lang.Object

↳android.os.PowerManager.WakeLock

A wake lock is a mechanism to indicate that your application needs to have the device stay on.Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an element of the application’s manifest. Obtain a wake lock by calling newWakeLock(int, String).

Wake Lock是一种机制,它能唤醒CPU,让系统无法进入休眠。如果要使用WakeLock需要在Manifest中添加如下权限:

Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.

Call release() when you are done and don’t need the lock anymore. It is very important to do this as soon as possible to avoid running down the device’s battery excessively.

获得wake lock,使用完要及时释放wake lock,否则会有耗电问题。

setReferenceCounted

void setReferenceCounted (boolean value)

Sets whether this WakeLock is reference counted.

Wake locks are reference counted by default. If a wake lock is reference counted, then each call to acquire() must be balanced by an equal number of calls to release(). If a wake lock is not reference counted, then one call to release() is sufficient to undo the effect of all previous calls to acquire().

是否使用引用计数。类似于垃圾回收策略,只是把垃圾回收改成了WakeLock回收。如果value是true的话将启用该特性,如果一个WakeLock acquire了多次也必须release多次才能释放掉。但是如果release次数比acquire多则会抛出异常。如果value是false的话,则无论acquire1次还是多次,只需要release1次。默认是开启了引用计数的。

newWakeLock

PowerManager.WakeLock newWakeLock (int levelAndFlags,

String tag)

Creates a new wake lock with the specified level and flags.

The levelAndFlags parameter specifies a wake lock level and optional flags combined using the logical OR operator.

参数levelAndFlags表示WakeLock的level,可以通过OR组合使用,有以下几个level:

1、PARTIAL_WAKE_LOCK:Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

保证CPU保持高性能运行,而屏幕和键盘背光关闭。一般情况下都会使用这个WakeLock。

2、ACQUIRE_CAUSES_WAKEUP:Turn the screen on when the wake lock is acquired.

除了会使CPU高性能运行外还会导致屏幕亮起,即使屏幕原先处于关闭的状态下。

3、ON_AFTER_RELEASE:When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.

如果释放WakeLock的时候屏幕处于亮着的状态,则在释放WakeLock之后让屏幕再保持亮一小会。如果释放WakeLock的时候屏幕本身就没亮,则不会有动作。

4、PROXIMITY_SCREEN_OFF_WAKE_LOCK:Turns the screen off when the proximity sensor activates.

实现打电话时靠近面部会灭屏,离开时又能使屏幕亮起来的功能。在API 21之前是隐藏的。

弃用的WakeLock:

1、FULL_WAKE_LOCK:Ensures that the screen and keyboard backlight are on at full brightness.

保证屏幕和键盘全亮,并且是最高亮度。(API level 17弃用)

2、SCREEN_DIM_WAKE_LOCK:Ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.

保证屏幕亮起,但是亮度可能比较低。同时键盘背光可以不亮。(API level 17弃用)

3、SCREEN_BRIGHT_WAKE_LOCK:Ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.

保证屏幕亮起,并且亮度最高。同时键盘背光可以不亮。(API level 13弃用)

以上弃用的WakeLock的作用都是保证屏幕长亮,推荐使WindowFlag WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/35343.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注