-
/**
-
* 唤醒手机屏幕并解锁
-
*/
-
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
-
-
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);
-
}
-
/**
-
* 唤醒手机屏幕并解锁
-
*/
-
public void wakeUpAndUnlock(Activity activity) {
-
-
Window win = activity.getWindow();
-
win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
-
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
-
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
-
-
PowerManager pm = (PowerManager) activity.getSystemService(POWER_SERVICE);
-
-
PowerManager.WakeLock wakelock = pm.newWakeLock(
-
PowerManager.FULL_WAKE_LOCK
-
| PowerManager.ACQUIRE_CAUSES_WAKEUP, “xx”);
-
wakelock.acquire();
-
wakelock.release();
-
-
KeyguardManager keyguardManager = (KeyguardManager) activity.getApplicationContext()
-
.getSystemService(KEYGUARD_SERVICE);
-
-
if (mHomeActivity == null) return;
-
keyguardManager.requestDismissKeyguard(mHomeActivity, new KeyguardManager.KeyguardDismissCallback() {
-
-
public void onDismissError() {
-
super.onDismissError();
-
Log.d(“xxx–>”, “1 onDismissError”);
-
}
-
-
-
public void onDismissSucceeded() {
-
super.onDismissSucceeded();
-
Log.d(“xxx–>”, “1 onDismissSucceeded”);
-
}
-
-
-
public void onDismissCancelled() {
-
super.onDismissCancelled();
-
Log.d(“xxx–>”, “1 onDismissCancelled”);
-
}
-
});
-
-
if (mRemoteActivity == null) return;
-
keyguardManager.requestDismissKeyguard(mRemoteActivity, new KeyguardManager.KeyguardDismissCallback() {
-
-
public void onDismissError() {
-
super.onDismissError();
-
Log.d(“xxx–>”, “2 onDismissError”);
-
}
-
-
-
public void onDismissSucceeded() {
-
super.onDismissSucceeded();
-
Log.d(“xxx–>”, “2 onDismissSucceeded”);
-
}
-
-
-
public void onDismissCancelled() {
-
super.onDismissCancelled();
-
Log.d(“xxx–>”, “2 onDismissCancelled”);
-
}
-
});
-
-
}
转载:
https://blog.csdn.net/xinpengfei521/article/details/73065769/