6

The problem My Android automated tests are unreliable, because the tested activities are sometimes running and are sometimes paused.

The cause If my android device (a Samsung Galaxy S2) has its Lock Screen on, all activities are immediately put in Pause mode.

How can I programmatically disable the Lock Screen while my tests are running?

Here is what I tried and that produced no result at all (i.e., Lock Screen still active and pausing my activities).

  • Add the android.permission.DISABLE_KEYGUARD permission and use KeyguardLock.disableKeyguard() as explained in the android doc.

  • Set Window flags: FLAG_SHOW_WHEN_LOCKED, FLAG_TURN_SCREEN_ON, FLAG_DISMISS_KEYGUARD.

  • Use WakeLock.acquire() with PowerManager.FULL_WAKE_LOCK, PowerManager.ACQUIRE_CAUSES_WAKEUP, PowerManager.ON_AFTER_RELEASE and PowerManager.SCREEN_BRIGHT_WAKE_LOCK parameters.

  • adb shell input keyevent 82

lacton
  • 2,316
  • 2
  • 22
  • 24
  • Please refer to my answer : http://stackoverflow.com/questions/12021800/disable-delay-android-lock-screen-programmatically/19955030#19955030 – Swapnil Kale Feb 03 '16 at 13:09

4 Answers4

6

You can disable your lockscreen by using following code.Include this code in oncreate of your activity .

PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "INFO");
        wl.acquire();

KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        kl = km.newKeyguardLock("name");
        kl.disableKeyguard();

In manifest include permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Madhu
  • 298
  • 1
  • 12
  • 3
    What you describe corresponds to the third and first solutions (respectively) I tried. They have no effect at all on the Lock Screen of my Samsung Galaxy S2. – lacton Sep 19 '13 at 18:31
6

In Kotlin,

For Api level 28 or less, you can simply add below method in your activity that needs to be opened:

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    toBeShownOnLockScreen()
}

private fun toBeShownOnLockScreen() {
    window.addFlags(
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    )
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setTurnScreenOn(true)
        setShowWhenLocked(true)
    } else {
        window.addFlags(
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        )
    }
}

And to make it work on Android Pie and above, in addition to above step, we need to set in AndroidManifest as well:

<activity
    android:name=".view.activity.LockScreenActivity"
    android:showOnLockScreen="true"
    android:showWhenLocked="true"
    android:turnScreenOn="true" />

I have tested this code from Api level 21 to 29, and works like charm!

Update: some Samsung devices (Samsung Galaxy S7 edge in my case) may fail to work if toBeShownOnLockScreen() is called inside onAttachedToWindow(). So, there you simply call toBeShownOnLockScreen() inside onCreate() of Activity. That's it.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • I noticed that after one lock and unlock UI starts to behave strangely - for example, messenger buble disappears, and after the second lock UI freezes. Something underneath happens but I cannot see it - so I click the button, something happens but I cannot see the result. – Witold Kupś May 27 '21 at 09:26
  • Thank you very much, it's working for me. Just a quick note: why did you duplicate the `showWhenLocked` and `turnScreenOn` attributes in the XML? You've already activated them in the Java code. – Mouaad Abdelghafour AITALI Aug 24 '23 at 12:01
  • @MouaadAbdelghafourAITALI Didn't get a chance to look into this. But AFAIK, the required was achieved only by writing at both places. Can you re-verify and update here in comments, if the code is working or not. – Chintan Soni Aug 29 '23 at 11:26
5
android:keepScreenOn='true'

use this attribute in rootView like this

<LinearLayout 
xmlns:android=`http://schemas.android.com/apk/res/android`
    xmlns:app=`http://schemas.android.com/apk/res-auto`
    android:layout_width=`match_parent`
    android:layout_height=`match_parent`
    android:keepScreenOn="true">
mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
Vijay Kumar
  • 59
  • 1
  • 3
0

Up to date Kotlin code for skipping the lock screen:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
    val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
    keyguardManager.requestDismissKeyguard(this, null)
    setShowWhenLocked(true)
} else {
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
}

For more elaborate explanation on keyguard flags, go here.

lomza
  • 9,412
  • 15
  • 70
  • 85