美文网首页
Android Wake Lock机制

Android Wake Lock机制

作者: isharley | 来源:发表于2017-07-27 14:58 被阅读0次

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中添加如下权限:

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

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

相关文章

  • Android Wake Lock 机制

    Android为了确保应用程序中关键代码的正确执行,提供了WakeLock的API,使得应用程序有权限通过代码阻止...

  • Android Wake Lock机制

    PowerManager.WakeLock public final class PowerManager.Wak...

  • Android Wake Lock 机制

    Android为了确保应用程序中关键代码的正确执行,提供了WakeLock的API,使得应用程序有权限通过代码阻止...

  • Android 使用Wake Lock

    为了延长电池的使用寿命,Android设备会在一段时间后使屏幕变暗,然后关闭屏幕显示,最后停止CPU。WakeLo...

  • January~fiveteen dream

    In three o'lock,I wake up.I dreamed I come to high school...

  • WakeLock

    PROXIMITY_SCREEN_OFF_WAKE_LOCK:pSensor导致的灭屏情况下系统不会进入休眠,正常...

  • Java性能 -- CAS乐观锁

    synchronized / Lock / CAS synchronized和Lock实现的同步锁机制,都属于悲观...

  • Android上保持Socket长连接

    0.Thanks 性能优化十六之Wake_Lock唤醒锁以及JobScheduler使用安卓 java 判断soc...

  • Android 事件分发机制源码

    Android 事件分发机制源码 Android,事件机制,Android事件分发机制源码 Android Tou...

  • 网站架构技术笔记

    proxy_cache:使用内存给/SSD级代理缓存内容 proxy_cache_lock:使用lock机制,将多...

网友评论

      本文标题:Android Wake Lock机制

      本文链接:https://www.haomeiwen.com/subject/rlkzkxtx.html