Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unlock the screen programmatically in Android?

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?

like image 843
SingleShot Avatar asked Dec 24 '09 16:12

SingleShot


People also ask

How do you unlock a locked Android screen?

How to remove Screen Lock on your Android phone. Tap Settings > Security > Screen Lock. If prompted, enter your current lock screen code > None > Delete.

How do I unlock my phone with tapping the screen?

Double-Tap to Lock and Unlock Android This feature can also be used to lock your phone—double-tap to lock, double-tap to unlock. Alternatively, if double-tapping your screen to lock and unlock your Android still isn't working, you can use a third-party app like Screen Off.


1 Answers

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.

like image 86
Dave Webb Avatar answered Sep 23 '22 21:09

Dave Webb