How to Lock and Unlock the Android Screen via ADB

How to Lock and Unlock the Android Screen via ADB

22 April 2024 Stephan Petzl Leave a comment Tech-Help

For developers and testers, the Android Debug Bridge (ADB) is an invaluable tool that allows you to communicate with an Android device. A common task that might be performed during testing is to lock or unlock the screen programmatically. This article will guide you through the process of doing so using ADB commands.

Locking the Android Screen

To simulate a display timeout or instantly lock the screen of an Android device from your PC, you can use the following ADB command:

adb shell input keyevent 26

This command simulates pressing the power button and will lock the screen if it is currently unlocked.

Unlocking the Android Screen

If the screen is already locked, the same command:

adb shell input keyevent 26

will wake up the device. However, depending on the device and Android version, you may need to perform a swipe action to unlock the screen after waking the device. This can vary based on the device’s screen size and orientation. For example:

adb shell input touchscreen swipe 930 380 1080 380

This command swipes across the screen from one coordinate to another, which can unlock the device if no secure lock screen is set up.

Checking Screen State

It is possible to check if the screen is on or off before sending the lock or unlock command. This can be done by querying the device’s power state:

adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)'

This command outputs ‘true’ if the screen is on and ‘false’ if it is off. You can use this information in a script to decide whether to send a lock or unlock command.

Automating Lock and Unlock with a Script

You can combine these commands into a script that checks the screen state and then locks or unlocks the screen accordingly. Here’s a simple example:


#!/bin/bash
screenState=$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')
if [ "$screenState" == "false" ] ; then
    echo "Screen is off. Turning on."
    adb shell input keyevent 26 # wakeup
    adb shell input touchscreen swipe 930 380 1080 380 # unlock
    echo "OK, should be on now."
else 
    echo "Screen is already on. Locking."
    adb shell input keyevent 26 # sleep
fi
        

Note: The swipe coordinates may need to be adjusted for your specific device.

Integrating with “Repeato”

While the above methods are great for manual testing, automating these processes can save time and ensure consistency. “Repeato” is a no-code test automation tool that can help you automate these tasks in your iOS and Android applications. With its computer vision and AI capabilities, “Repeato” can simulate user interactions on any app framework, making it a versatile tool for developers and testers alike. Moreover, it comes with ADB on board and allows execution of ADB commands via “script steps,” making it perfect for scenarios where you need to lock or unlock the screen as part of your automated testing sequences.

By integrating “Repeato” into your workflow, you can streamline your testing process, allowing you to focus on more complex tasks while it handles the repetitious work of locking and unlocking screens, among other actions.

For more information on ADB and automation testing, consider visiting our articles on Troubleshooting ADB Device Unauthorized Issues and Launching Android Applications via ADB.

Like this article? there’s more where that came from!