3

I am using Keyguard Manager in my activity to unlock the device, but it is behaving strangely. I tested it with two devices, on Nexus its working fine but in samsung Galaxy S, it's not working.

My Code is:

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();

Can anyone Help?

Chris
  • 2,955
  • 1
  • 30
  • 43
Meenal
  • 2,879
  • 5
  • 19
  • 43

1 Answers1

4

I found this solution,here mContext is the context of your activity,i am using it outside the activity.

    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();
    }
albertosh
  • 2,416
  • 7
  • 25
  • 32
Meenal
  • 2,879
  • 5
  • 19
  • 43
  • Is the check for version correct? It seems to be say if the version is <= FROYO do the code for **new** versions? Shouldn't it be the other way around? – mike jones Nov 05 '13 at 22:28
  • Is it safe to use KeyguardManager ahead of PowerManager? – NeviQ-OR Nov 12 '14 at 22:26
  • I have the same problem with KeyguardManager on Nexus mostly. If i disabled it in a lockscreen, and press the running tasks button, and so move to another app in the stack, i cannot use the keyboard at all (it sometimes appear) and on reboot, I often see a black screen. How did you resolved this (i know it's an old thread) ? thx – Gabi Radu Jan 22 '15 at 08:09