如何通过代码解锁Android 6.0

时间:2016-08-14 13:37:00

标签: android android-screen keyguard

我想通过编程解锁我的Android手机。我在Android 6.0下面使用了以下代码,但它有一些问题

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
    kl.disableKeyguard();

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
    wakeLock.acquire();

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

因为不推荐使用KeyguardLock,所以我使用下面的代码

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

但它有错误

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)
at android.view.View.requestLayout(View.java:17483)
at android.view.View.setLayoutParams(View.java:11478)
at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:305)
at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:91)
at android.app.Activity.onWindowAttributesChanged(Activity.java:2596)
at android.support.v7.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:108)
at android.view.Window.dispatchWindowAttributesChanged(Window.java:852)
at com.android.internal.policy.impl.PhoneWindow.dispatchWindowAttributesChanged(PhoneWindow.java:4252)
at android.view.Window.setFlags(Window.java:825)
at android.view.Window.addFlags(Window.java:771)

如何解决?其次,如果我的手机被密码锁定,我该如何解锁?谢谢

1 个答案:

答案 0 :(得分:-1)

权限问题,您可以添加权限并授予此权限,因为棉花糖需要授予权限,您的代码在棉花糖完美之前正常工作。