views:

580

answers:

2

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?

+4  A: 

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.

Dave Webb
Doesn't work for me. According to http://bit.ly/6QjamY Menu button is KEY_SOFT1 and not KEY_MENU.
Mirko Nasato
I tested it on my 1.5 AVD and it seemed to work OK. Does KEY_SOFT1 work for you?
Dave Webb
Tested it again on a 1.6 image. Using "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" _does_ unlock a newly launched AVD for me. But if you run it again it's not emulating the menu key. Not sure what it is doing but I guess it's still a sort of solution to the problem. Will probably take a longer look at this after Christmas.
Dave Webb
SOFT1 clearly is Menu for me, but to unlock screen I need to press another key first e.g. HOME, then SOFT1. Still not reliable though, needs more investigation.
Mirko Nasato
A: 

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
Yimin Li