Programmatically turn screen on in android

21,396

Solution 1

I started off much like you, and the window flags didn't really work.

I finally got it to work by using two Android services: KEYGUARD_SERVICE and POWER_SERVICE:

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

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

Don't forget to release the wake lock when you're done with it.

You'll need to request permissions DISABLE_KEYGUARD and WAKE_LOCK

Solution 2

Here is the solution

WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT,
            LayoutParams.TYPE_SYSTEM_ALERT |
                    LayoutParams.TYPE_SYSTEM_OVERLAY,
                    LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    LayoutParams.FLAG_KEEP_SCREEN_ON|
                    LayoutParams.FLAG_DISMISS_KEYGUARD|
                    LayoutParams.FLAG_TURN_SCREEN_ON,
            PixelFormat.TRANSPARENT);

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    km.isKeyguardLocked();

    windowManager.addView(mTopView, params);
    getWindow().setBackgroundDrawable();

Solution 3

I also suffered from many problems for my app. Actually i want screen lock when user presses the back button twice. and unlock when user presses the home button.

For unlock the device marmor's code is right. :) I've used this code.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
                WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
                wakeLock.acquire();
Share:
21,396
user1177122
Author by

user1177122

Updated on March 09, 2020

Comments

  • user1177122
    user1177122 about 4 years

    I am developing an alarm application. From the main activity i set the alarm using broadcast. Then in onReceive in broadcast receiver i call activity that is enabling user to shut down or snooze the alarm...In that activity, in the beginning of onCreate i use this lines to turn screen on and unlock the device:

    final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                      | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                      | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    

    This works perfect on Samsung Galaxy S2 with android 2.3.4 but doesnt work on htc with android 2.3.5. On htc it does nothing, and when i press lock button screen automatically unlocks without me draging the circle. Its like flag_dissmiss_keygard is working but flag_turn_screen_on isnt. Is there another way or another solution for doing this?

  • nifo
    nifo over 11 years
    I tried it on Nexus One (2.3.6) and Nexus S (4.1.1) and it works pretty well.
  • Patrick Horn
    Patrick Horn over 11 years
    Developers, I beg you: please don't do this!!! Just want to put out a warning that disabling keyguard does what it says: it bypasses any PIN or Pattern unlock. Try a long-press of home (gingerbread) or app switch button (ICS) and you will see what I mean. I have had no issues using layout params on the most popular phones, and the number of Gingerbread phones is going down daily. Please reconsider window flags!
  • marmor
    marmor over 11 years
    Hey @PatrickHorn can you specify an example of a device in which this bypasses the PIN lock screen. In my tests I can display a screen to the user with the message I want, and the second the user tries to hit home or switch app, he gets the enter PIN screen. So it seemed safe enough to me.
  • ChuongPham
    ChuongPham over 10 years
    In situation where the user has created a secured lock for their device (i.e. by using a PIN, password, etc), I would be wary of using the KeyguardLock#disableKeyguard() part of the answer above as this would "seem" to bypass the screen lock on their device. Also, the WindowManager#LayoutParams() method is a hit-and-run in that it doesn't always work even though Google framework developers thought so and have commented as such on other posts here in SO.
  • Manza
    Manza over 8 years
    In my device this solution only turn on the screen BUT do not unlock android. Do I need to add somethin?
  • Kishan B Manavadariya
    Kishan B Manavadariya over 8 years
    @Manza Use the code from below answer given by the marmor. That will unlock the device too.
  • android developer
    android developer over 7 years
    Does this require any permission?
  • koteswarao
    koteswarao over 7 years
    Sorry for delay, i have given the permission in AndroidManifest.xml <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
  • Alexey Ozerov
    Alexey Ozerov over 3 years
    What is mTopView and what does the call to km.isKeyguardLocked() ?
  • Devin Carpenter
    Devin Carpenter over 2 years
    Unfortunately FULL_WAKE_LOCK is now deprecated