/**
* 唤醒手机屏幕并解锁
*/
public void wakeUpAndUnlock() {
// 获取电源管理器对象
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
boolean screenOn = pm.isScreenOn();
Log.d("WakeScreen0","screenOn: " + screenOn);
if (!screenOn) {
// 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
@SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
wl.acquire(10000); // 点亮屏幕
wl.release(); // 释放
}
// 屏幕解锁
KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext()
.getSystemService(KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
// 屏幕锁定
// keyguardLock.reenableKeyguard();
keyguardLock.disableKeyguard(); // 解锁
unLockScreen();
}
private void unLockScreen(){
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
转载:
如果觉得《android 触摸 唤醒屏幕 android 唤醒和解锁屏幕》对你有帮助,请点赞、收藏,并留下你的观点哦!