Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock Android screen via ADB?

Is there a way to lock the Android screen via the ADB?

I find ways to lock the display in an apk, but I want to lock the screen from the PC via ADB, to simulate a display timeout, without having to wait for the timeout.

Is it possible to do this?

Thanks, Diane

like image 591
PurpleDiane Avatar asked Dec 12 '12 23:12

PurpleDiane


People also ask

Is there a way to lock an Android screen?

Find and tap Settings > Lock screen & security > Screen lock > Password. Enter a screen lock password (at least 4 characters), then tap NEXT. Re-enter the password, then tap CONFIRM.

How do I manually lock my Android screen?

If "Smart Lock" is active and "Power button instantly locks" is set, then you can manually lock the screen by tapping the "lock icon" on the bottom of the lock screen. Show activity on this post. 2) Press the device power button twice in quick succession.


2 Answers

Cool, I just found KEYCODE_POWER which is 26.

so it works by sending:

adb shell input keyevent 26 

which locks the screen if the screen is unlocked. If the screen is already locked, it wakes up the device.

My guess is that the only way to ensure that the screen is locked (off), is to unlock (we use keyevent 82 (menu), then lock it with the power button keyevent. Does anyone have any idea if this is true?

like image 167
PurpleDiane Avatar answered Sep 18 '22 21:09

PurpleDiane


Michael R. Hines gave the what is arguably the easiest solution. However, the following line is not useful in later versions of Android.

adb shell input keyevent 82 # unlock 

I've updated the shell script using coordinates for the individual device I want to wake (Tablet). My tablet does not support orientation changes for lockscreen events, so the values always work as the lockscreen is always in landscape. Should you require orientation change detection, a simple if/then/else would suffice in picking the correct coordinates to use for the orientation.

#!/bin/bash if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == 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."     echo "Turning off."     adb shell input keyevent 26 # sleep fi 
like image 21
Rescue9 Avatar answered Sep 18 '22 21:09

Rescue9