KeyguardManager无法解锁kitkat屏幕

时间:2014-11-12 22:44:39

标签: android keyguard keyguardlock android-powermanager

我注意到Android的非常有趣的行为,我无法解释。我使用以下代码唤醒手机并禁用键盘锁:

        PowerManager.WakeLock mFullWakelock = mPowerManager.newWakeLock(
                (PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
                        PowerManager.FULL_WAKE_LOCK |
                        PowerManager.ACQUIRE_CAUSES_WAKEUP),
                LOCK_TAG
        );

        mFullWakelock.acquire();

        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(LOCK_TAG);

        keyguardLock.disableKeyguard();

想象一下有唤醒循环(以编程方式) - >禁用键盘锁(以编程方式) - >按下电源按钮(手动) - >唤醒 - >禁用键盘锁。这个循环很有效,直到我在手机的键盘锁被禁用时手动按Home键。之后,手机会唤醒,但不再以编程方式禁用键盘锁。我很欣赏任何想法!

2 个答案:

答案 0 :(得分:0)

问题是,只要用户按下主页按钮或打开通知,键锁似乎就会过期。所以每当发生这种情况时,你必须创建一个新的键锁。

我使用了这个解决方案,效果很好: https://stackoverflow.com/a/14519861/4098821

答案 1 :(得分:0)

我遇到了同样的问题,我使用反射解决了这个问题,并将我的应用程序作为系统签名应用程序。

以下是代码:

try{
                                   Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
                                   Constructor lockPatternUtilsConstructor = 
                                       lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
                                   lockPatternUtilsConstructor.setAccessible(true);
                                   Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(ChangeDeviceLockMode.this);
                                   Method clearLockMethod = lockPatternUtils.getClass().getMethod("clearLock", boolean.class);
                                   clearLockMethod.setAccessible(true);
                                   Method setLockScreenDisabledMethod = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class);
                                   setLockScreenDisabledMethod.setAccessible(true);
                                   clearLockMethod.invoke(lockPatternUtils, false);
                                   setLockScreenDisabledMethod.invoke(lockPatternUtils, true);     
                                   Toast.makeText(ChangeDeviceLockMode.this,"none", Toast.LENGTH_LONG).show();  
                           }catch(Exception e){
                               System.err.println("An InvocationTargetException was caught!");
                                                   Throwable cause = e.getCause();
                                                   Toast.makeText(ChangeDeviceLockMode.this,"none--"+cause, Toast.LENGTH_LONG).show();  
                               Toast.makeText(ChangeDeviceLockMode.this,"none--"+e, Toast.LENGTH_LONG).show();  
                           }

您还需要在清单中添加权限

android:name="android.permission.ACCESS_KEYGUARD_SECURE_STORAGE" />

此权限要求应用程序进行系统签名。