一、解锁
//屏锁管理器
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
二、获取电源锁,保持该服务在屏幕熄灭时仍然能唤醒
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
//点亮屏幕
//wakeLock.acquire(10*60*1000L /*10 minutes*/);
wakeLock.acquire();
三、权限申请
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
四、各种锁的类型对CPU 、屏幕、键盘的影响:
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间
五、实例测试
package com.giada.wakeup;
import androidx.appcompat.app.AppCompatActivity;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;
import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
public class MainActivity extends AppCompatActivity {
String TAG="WAKEUP";
PowerManager.WakeLock wakeLock = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wakeUpAndUnlock(this);
}
public static void wakeUpAndUnlock(Context context){
//屏锁管理器
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
//获取电源管理器对象
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
//点亮屏幕
//wakeLock.acquire(10*60*1000L /*10 minutes*/);
wakeLock.acquire();
//释放
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
wakeLock.release();
}
}, 20000);//3秒后执行Runna
// wakeLock.release();
}
private void wakeUp() {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
if (!powerManager.isInteractive()) {
//PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "wakeupdemo");
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "wakeupdemo");
//点亮屏幕
//wakeLock.acquire(1000L);
wakeLock.acquire(10*60*1000L /*10 minutes*/);
//释放
// wakeLock.release();
}
}
}
六、实际测试,在屏幕熄灭进入休眠后,用adb 运行这个app, 屏幕会亮起来,过几秒会稍微暗(因为SCREEN_DIM_WAKE_LOCK参数,如果想一直高亮可以用SCREEN_BRIGHT_WAKE_LOCK),20秒之后执行release后就又进行休眠。
七、有价值的参考文章
Android应用程序保持后台唤醒(使用WakeLock实现)_小zhong的博客-CSDN博客_android通知唤醒