0

I want to unlock my Android phone by programming. I used below code for Android 6.0 but it has some issue

    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"/>

Because the KeyguardLock is deprecated, thus I use below code

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

but it has an error

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)

How to fix it? Second, if my phone is locked by password, How can I unlock it? Thank you

Jame
  • 3,746
  • 6
  • 52
  • 101

1 Answers1

-1

Permission problem, you can add permission and grant this permission , because marshmallow need grant permission,your code is working before marshmallow perfect.

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
  • Thank. Could you edit the permission to work in all android version? I tested in Android 5.0, it has one more issue that phone is unlock but screen is not light – Jame Aug 14 '16 at 13:42
  • Search on stock overflow how to unlock device..you will get answer – Yogesh Rathi Aug 14 '16 at 14:24
  • I searched in stackoverflow and got above code. It unlock my phone but does not wakeup the light – Jame Aug 14 '16 at 14:25
  • Good. It works but when I use PARTIAL_WAKE_LOCK, instead of FULL_WAKE_LOCK. The light does not work, although the screen unlocks. I think I will use your code, with some warning about function FULL_WAKE_LOCK – Jame Aug 14 '16 at 14:42
  • Do you know why I cannot use PARTIAL_WAKE_LOCK, although many people suggest it – Jame Aug 14 '16 at 14:44