How to avoid manually turning on, sliding and unlocking screen each time I run Android app remotely
Using the standard Eclipse-based development environment to develop Android apps, you press Control-F11 to squirt your app down the USB cable to your phone. This works great, but if you've done practically any work at all between tests the screen will have timed out and turned off. You then have to press the power button, then slide the slider, then do the pattern unlock pattern, and only then can 开发者_运维问答you see your app running. This gets old pretty quickly.
It would be nice to be able to somehow have a message sent down with your app to do all this for you.
I accept that I could probably configure my phone to not turn the screen off, and disable the slider and pattern lock, but I don't really want to as I always want that behaviour when I'm not developing.
Any idea if the behaviour I'm after is possible? I've looked unsuccessfully for any documentation describing how to customize whatever happens when you send data to the phone. If possible the solution should be possible from within eclipse, but even having to send it via Terminal (I use Ubuntu to develop) or something would similar would be better than nothing.
Just add the following code to the onCreate method of your starting activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
This way android turns the screen on when your app comes to the foreground and it releases the wakelock automatically (you don't have to release it manually).
You don't have to add any permissions (as when using PowerManager to acquire a wakelock) to the manifest and may of course delete/comment out the line when finished with debugging.
If you go into Settings > Applications > Development there is an option there named "Stay Awake" which means the screen never sleeps while charging.
There is also another useful option named "USB Debugging" which should also be turned on if you are developing and testing over USB.
Hope that helps,
Alan
精彩评论