我在我的活动中使用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();
任何人都可以帮忙吗?
答案 0 :(得分:4)
我找到了这个解决方案,这里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();
}