PowerManager之WakeLock源码解析

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

首先看源码注释里对WakeLock类的注释:

    /**
     * A wake lock is a mechanism to indicate that your application needs
     * to have the device stay on.
     * <p>
     * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
     * permission in an {@code <uses-permission>} element of the application's manifest.
     * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
     * </p><p>
     * Call {@link #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.
     * </p><p>
     * Call {@link #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.
     * </p>
     */


用来控制设备保持运行状态的,需要配置申请权限,获取实例通过newWakeLock方法、使用acquire获取唤醒锁保持设备运行、使用release进行释放

加锁方式有两种,一种为永久锁,需要用户手动释放,另一种是超时锁,到时间后自动释放,源码如下

        /**
         * Acquires the wake lock.
         * <p>
         * Ensures that the device is on at the level requested when
         * the wake lock was created.
         * </p>
         */
        public void acquire() {
            synchronized (mToken) {
                acquireLocked();
            }
        }

      /**
         * Acquires the wake lock with a timeout.
         * <p>
         * Ensures that the device is on at the level requested when
         * the wake lock was created.  The lock will be released after the given timeout
         * expires.
         * </p>
         *
         * @param timeout The timeout after which to release the wake lock, in milliseconds.
         */
        public void acquire(long timeout) {
            synchronized (mToken) {
                acquireLocked();
                mHandler.postDelayed(mReleaser, timeout);
            }
        }

mReleaser是释放锁的Runnable

        private final Runnable mReleaser = new Runnable() {
            public void run() {
                release();
            }
        };


再回到前面看加锁的源码acquireLocked:

        private void acquireLocked() {
            if (!mRefCounted || mCount++ == 0) {
                // Do this even if the wake lock is already thought to be held (mHeld == true)
                // because non-reference counted wake locks are not always properly released.
                // For example, the keyguard's wake lock might be forcibly released by the
                // power manager without the keyguard knowing.  A subsequent call to acquire
                // should immediately acquire the wake lock once again despite never having
                // been explicitly released by the keyguard.
                mHandler.removeCallbacks(mReleaser);
                try {
                    mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource);
                } catch (RemoteException e) {
                }
                mHeld = true;
            }
        }

mRefCounted是用来控制是否关联计数的变量,默认为true,如果设置为false则后面的计数变量mCount一直不会改变

  private boolean mRefCounted = true;


下面来看释放锁的源码release

        /**
         * Releases the wake lock.
         * <p>
         * This method releases your claim to the CPU or screen being on.
         * The screen may turn off shortly after you release the wake lock, or it may
         * not if there are other wake locks still held.
         * </p>
         */
        public void release() {
            release(0);
        }

        /**
         * Releases the wake lock with flags to modify the release behavior.
         * <p>
         * This method releases your claim to the CPU or screen being on.
         * The screen may turn off shortly after you release the wake lock, or it may
         * not if there are other wake locks still held.
         * </p>
         *
         * @param flags Combination of flag values to modify the release behavior.
         * Currently only {@link #WAIT_FOR_PROXIMITY_NEGATIVE} is supported.
         *
         * {@hide}
         */
        public void release(int flags) {
            synchronized (mToken) {
                if (!mRefCounted || --mCount == 0) {
                    mHandler.removeCallbacks(mReleaser);
                    if (mHeld) {
                        try {
                            mService.releaseWakeLock(mToken, flags);
                        } catch (RemoteException e) {
                        }
                        mHeld = false;
                    }
                }
                if (mCount < 0) {
                    throw new RuntimeException("WakeLock under-locked " + mTag);
                }
            }
        }


所以不计数模式,无论acquire多少次,只需调一次release就可以进行释放;而计数模式每调一次acquire,只是把计数变量值加1,释放时也需要--mCount == 0时才能释放,调用次数过多,导致mCount<0会throw new RuntimeException("WakeLock under-locked " + mTag);而不计数模式不会出现,因为mCount一直为初始值0

那设置计数模式的地方在哪里呢,看下面

        /**
         * Sets whether this WakeLock is reference counted.
         * <p>
         * Wake locks are reference counted by default.  If a wake lock is
         * reference counted, then each call to {@link #acquire()} must be
         * balanced by an equal number of calls to {@link #release()}.  If a wake
         * lock is not reference counted, then one call to {@link #release()} is
         * sufficient to undo the effect of all previous calls to {@link #acquire()}.
         * </p>
         *
         * @param value True to make the wake lock reference counted, false to
         * make the wake lock non-reference counted.
         */
        public void setReferenceCounted(boolean value) {
            synchronized (mToken) {
                mRefCounted = value;
            }
        }


注释已经很详细了

在实际开发中,可以这样简单使用,设置为不计数模式,然后在onResume中acquire,在onPause中release,用来控制在某个页面保持屏幕恒亮。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
author:杨兴达; 邮箱:[email protected] 电话:180 1018 0585 Android 电源管理 -- wakelock机制,通过控制wakelock 实现保持pad 禁止休眠状态; Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠, 可以被用户态程序和内核获得. 这个锁可以是有超时的 或者 是没有超时的, 超时的锁会在时间过去以后自动解锁。如果没有锁了或者超时了, 内核就会启动休眠的那套机制来进入休眠. PowerManager.WakeLock 有加锁和解锁两种状态,加锁的方式有两种: 第一种是永久的锁住,这样的锁除非显式的放开,否则是不会解锁的,所以这种锁用起来要非常的小心。 第二种锁是超时锁,这种锁会在锁住后一段时间解锁。 在创建了 PowerManager.WakeLock 后,有两种机制,第一种是不计数锁机制,另一种是计数锁机制。可以通过 setReferenceCounted(boolean value) 来指定,一般默认为计数机制。这两种机制的区别在于,前者无论 acquire() 了多少次,只要通过一次 release()即可解锁。而后者正真解锁是在( --count == 0 )的时候,同样当 (count == 0) 的时候才会去申请加锁。所以 PowerManager.WakeLock 的计数机制并不是正真意义上的对每次请求进行申请/释放每一把锁,它只是对同一把锁被申请/释放的次数进行了统计,然后再去操作。 源码 位置:frameworks/base/core/java/android/os/PowerManager.java ++++++++++++++++++++++++ 讲述 应用层 申请的锁 怎么传到kernel下面的,来理解 整个wakelock的框架。 比如android跑起来之后 在 /sys/power/wake_lock 下面的PowerManagerService 的生成过程。 1).应用程序申请锁 Android 提供了现成 android.os.PowerManager 类 , 类中 提供newWakeLock(int flags, String tag)方法 来取得 应用层的锁, 此函数的定义 frameworks/base/core/java/android/os/PowerManager.java 应用程序 在申请wake_lock时 都会有调用以下 部分。 实例: PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, “MyTag”); wl.acquire();//申请锁,这里会调用PowerManagerService里面acquireWakeLock() *********************** wl.release(); //释放锁,显示的释放锁,如果申请的锁不在此释放,系统就不会进入休眠。 ====================================== 2). frameworks层 /frameworks/base/services/java/com/android/server/PowerManagerService.java这个类是来管理 所有的应用程序 申请的wakelock。比如音视、频播放器、camera等申请的wakelock 都是通过这个类来 管理的。 static final String PARTIAL_NAME ="PowerManagerService" nativeAcquireWakeLock(PARTIAL_WAKE_LOCK_ID, PARTIAL_NAME); 上面 这个函数调用Power类 里面的 acquireWakeLock(),此时的PARTIAL_NAME作为参数传递到底层去。 public static native void nativeAcquireWakeLock(int lock, String id); 注:在PowerManagerService 类中没有实现nativeAcquireWakeLock,其实现体在 frameworks/base/core/jni/android_os_Power.cpp中,所以nativeAcquireWakeLock()方法时会调用JNI下的实现方法。 3).JNI层的实现 路径:frameworks/base/core/jni/android_os_Power.cpp // static void acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj) { ************** const char *id = env->GetStringUTFChars(idObj, NULL); acquire_wake_lock(lock, id); env->ReleaseStringUTFChars(idObj, id); } 注:在acquireWakeLock()中调用了 路径下hardware/libhardware_legacy/power/power.c下面的acquire_wake_lock(lock, id) 4).与kernel层的交互 在power.c下的acquire_wake_lock(lock, id)函数如下: int acquire_wake_lock(int lock, const char* id) { ************** return write(fd, id, strlen(id)); } 注: fd就是文件描述符,在此 表示”/sys/power/wake_lock” id就是从PowerManagerService类中传下来的参数即:PARTIAL_NAME = "PowerManagerService" 到此 就是通过 文件系统 来与kernel层 交互的地方。 +++++++++++++++++++++++++++++++++++++++++++++++++++++ PowerManager类被应用程序调用,控制电源设备状态切换: PowerManager类对外有三个接口函数: 1、void goToSleep(long time); //强制设备进入Sleep状态 Note:在应用层调用该函数, 应用需要 在 源码下编译,用系统签名,否则 调用 此函数 出错; 2、newWakeLock(int flags, String tag);//取得相应层次的锁 flags参数说明: PARTIAL_WAKE_LOCK :保持CPU 运转,屏幕和键盘灯是关闭的。 SCREEN_DIM_WAKE_LOCK :保持CPU 运转,允许保持屏幕显示但有可能是灰的,关闭键盘灯 SCREEN_BRIGHT_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,关闭键盘灯 FULL_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度 ACQUIRE_CAUSES_WAKEUP: 一旦有请求锁时,强制打开Screen和keyboard light ON_AFTER_RELEASE: 在释放锁时reset activity timer Note: 如果申请了partial wakelock,那么即使按Power键,系统也不会进Sleep,如Music播放时 如果申请了其它的wakelocks,按Power键,系统还是会进Sleep 3、void userActivity(long when, boolean noChangeLights);//User activity事件发生,设备会被切换到Full on的状态,同时Reset Screen off timer. PowerManagerWakeLock的操作步骤 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。 然后通过PowerManager的newWakeLock ((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。 获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他操作,最后使用release()释放(释放是必须的)。 Note: 1. 在使用以上函数的应用程序中,必须在其Manifest.xml文件中加入下面的权限: <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DEVICE_POWER" /> 2. 所有的锁必须成对的使用, 如果申请了而没有及时释放,会造成系统故障。如申请了partial wakelock,而没有及时释放, 那系统就永远进不了Sleep模式.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值