android-Keyguard Manager无法正常工作
作者:互联网
我在活动中使用Keyguard Manager来解锁设备,但是它的行为很奇怪.我用两种设备对其进行了测试,在Nexus上运行正常,但在三星Galaxy S上则无法运行.
我的代码是:
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),"mini");
wakeLock.acquire();
KeyguardManager keyguardManager = (KeyguardManager) ctx.
getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("mini");
keyguardLock.disableKeyguard();
谁能帮忙?
解决方法:
我找到了这个解决方案,其中mContext是您活动的上下文,我在活动之外使用它.
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.FROYO) {
// only for gingerbread and newer versions
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
KeyguardManager manager (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();
} else {
KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
keyguardLock.disableKeyguard();
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE
| PowerManager.SCREEN_BRIGHT _WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
}
标签:android,keyguard,keyguardlock 来源: https://codeday.me/bug/20191030/1971525.html