键盘管理器无法正常工作

时间:2013-05-13 10:59:48

标签: android keyguard keyguardlock

我在我的活动中使用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();

任何人都可以帮忙吗?

1 个答案:

答案 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();
    }