How can I unlock the screen programmatically in Android?

33,128

Solution 1

You can interact with the emulator via its console interface.

If you ever wondered why your emulator started with a number like 5554 - that's because that's the port the emulator listening on.

You can find the port for running emulators with the adb devices command. It will have output like this:

C:\>adb devices
List of devices attached
emulator-5554   device

So you can connect to the emulator using a command like:

telnet localhost 5554

If you connect successfully you'll get an OK prompt and you can start entering commands.

There are various commands but the one we are interested in is event to simulate hardware events. We can unlock the screen by pressing Menu which we emulate with the following command:

event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0

The EV_KEY:KEY_MENU:1 is key-down event and the EV_KEY:KEY_MENU:0 is the corresponding key-up event. Make sure you do both or the Menu key will be stuck down.

I realise scripting this will be far from easy, but it's all I can think of to solve your problem.

Edit: I don't think event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0 is emulating Menu but if I run the command just after I've started the emulator it does unlock it. Not sure why but I guess this is a start.

Solution 2

The one-liner that works for me on Android SDK r18 and r20 talking to a 4.0.3 (API 15) emulator:

adb shell input keyevent 82

Solution 3

I believe the following link has the answer you are looking for.

http://developer.android.com/guide/topics/testing/activity_testing.html#UnlockDevice

Unlocking the emulator or device

You may find that UI tests don't work if the emulator's or device's home screen is disabled with the keyguard pattern. This is because the application under test can't receive key events sent by sendKeys(). The best way to avoid this is to start your emulator or device first and then disable the keyguard for the home screen.

You can also explicitly disable the keyguard. To do this, you need to add a permission in the manifest file (AndroidManifest.xml) and then disable the keyguard in your application under test. Note, though, that you either have to remove this before you publish your application, or you have to disable it with code in the published application.

To add the the permission, add the element as a child of the element. To disable the KeyGuard, add the following code to the onCreate() method of activities you intend to test:

mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); mLock = mKeyGuardManager.newKeyguardLock("activity_classname"); mLock.disableKeyguard();where activity_classname is the class name of the activity.

Solution 4

Try this script:

echo "event send EV_KEY:KEY_SOFT1:1" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:0" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:1" | nc -q1 localhost 5554
sleep 0.1
echo "event send EV_KEY:KEY_SOFT1:0" | nc -q1 localhost 5554
sleep 0.1

Solution 5

The easiest way to unlock the screen (assuming no password) is to pass a KEYCODE_MENU event; however, if the screen is already unlocked, this will actually open a menu, which probably isn't the functionality you're looking for.

If you want a simple way to guarantee that the screen will be unlocked without opening a menu by accident, you should pass in a KEYCODE_POWER event first. This will guarantee that the device is in a locked state, so the menu event will only unlock the screen.

adb shell input keyevent KEYCODE_POWER; adb shell input keyevent KEYCODE_MENU

(Note: The 82 that appears in other responses is the KEYCODE_MENU constant.)

Share:
33,128
SingleShot
Author by

SingleShot

Software Engineer. Wargamer.

Updated on October 28, 2020

Comments

  • SingleShot
    SingleShot over 3 years

    I am working on a remote automated test framework for Android based on JUnit (tests run outside android, interacting with code inside it). It's all working fairly well, but one issue I have is that when I automatically start a fresh emulator, the screen starts out locked. This appears to affect my tests being able to run, plus, I want to watch the tests run (buttons clicked, text typed, etc.). If I manually start an emulator and unlock its screen, all works well.

    Is there a way to programmatically unlock the screen in Android? A Java API, a command line or shell command, etc. would all be fine. Barring that, perhaps there is a way to start an emulator unlocked?