控制CPU、屏幕等是否休眠

获取锁

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,

                this.getClass().getCanonicalName());
    wakeLock.acquire();

  释放锁:
    if (wakeLock !=null&& wakeLock.isHeld()) {

        wakeLock.release();wakeLock=null;}

  参数说明:PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。

       SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯

       SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯

       FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

       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.

       ON_AFTER_RELEASE:f 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.

  权限:<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
     可能还需要
<uses-permissionandroid:name="android.permission.DEVICE_POWER"/>


唤醒屏幕与解锁:

    WakeLock screenLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
        | PowerManager.ACQUIRE_CAUSES_WAKEUP
        | PowerManager.ON_AFTER_RELEASE, TAG);

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
     KeyguardLock keyguardLock = km.newKeyguardLock(TAG);
      if (km.inKeyguardRestrictedInputMode()) {
       keyguardLock.disableKeyguard(); // 解开屏幕锁

      }    

    keyguardLock.reenableKeyguard();  // 屏幕锁生效


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值