3

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

任何人都可以帮忙吗?

4

1 回答 1

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();
    }
于 2013-05-31T05:09:46.117 回答