Unlocking the emulator or device for Android test


I am writing Android functional tests. I need the device to be awake and the application to be visualized in order for them to work. I have consulted with Android developers for that. However, there are several problems with the proposed solution:

  • The proposed method now has several deprecated methods. Android API now advises me to use flags on the window:

This class was deprecated in API level 13. Use FLAG_DISMISS_KEYGUARD
and/or FLAG_SHOW_WHEN_LOCKED instead; this allows you to seamlessly
hide the keyguard as your application moves in and out of the
foreground and does not require that any special permissions be
requested. Handle returned by newKeyguardLock(String) that allows you
to disable / reenable the keyguard.

I tried this suggestion, however, as opposed to the deprecated solution, the flag one jsut does not work for me – it does not unlock the device. I also found sources confirming the instability of the flag solution (e.g. the comments on this answer).

  • The second problem is that I need to add permissions to the application under test. I find this as quite wrong approach (modifying the code under test in order to be able to test it). I found several places that advise me to use src/debug/AndroidManifest.xml for that (this one and this one for example). However, it just not happen as stated – the debug Mainfest does not get included to the deployed things on the device. Is it related to the way I build and deploy? I use Eclipse for development (ADT). Will this manifest injection work only if I use build tool like ant?

All in all – can somebody advise on a stable solution for unlcoking and keeping awake the device while my test are being executed?


I have now found that Robotium also included unlockScreen method in their latest version of the framework. It uses exactly the window flags proposed above and is also not working on my device.

  • You must to post comments

I’ve had the same problems with unreliable unlocking when running Robotium tests. I did not find a solution to programmaticly unlock devices that worked reliably, but there are two things that I did as a work-around.

  • Just disable the lock screen (Settings -> Security -> Screen lock -> None). Not ideal but at the end of the day, reliable tests are the important thing.

  • Enable “Stay Awake” settings to keep the screen from turning off due to inactivity (Settings -> Developer options -> Stay awake). Some OEM/phones either don’t have that option or still turn off anyways, so for those pesky devices I installed the KeepScreenOn app.

Warning: leaving the screen on, sitting on the home screen, for 24 hours a day may cause some screen burn-in/ghosting. I’m not sure if this is permanent, but be aware of this. In our case, we were using dedicated test devices so it was not a big deal.

Also note that since the phones will have their screen on at all times, you may want to dim the brightness to use less battery power (charging over USB can be slow sometimes).

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.