How do I keep my screen unlocked during USB debugging?

There are multiple options, many of which have HORRIBLE (subjective) side effects. I'll list what I've found and what side effects I could think of.

I'm lazy, possibly like many others, and I don't like to keep on eye out for something unneccessarily. This means that the "Oh, I'll just turn this on while I'm working and then turn it off when finished" option is not a viable one. You will forget it and you will experience any of the sideeffects listed below eventually.

TL;DR

FLAG_KEEP_SCREEN_ON with a Debug.isDebuggerConnected() guard FTW! Stay awake when charging, Screen timeout and wake-lock are non-viable options if you just want to debug.
There's also an advantage of this solution working from API 1 AND wifi debugging!

Settings > Developer Options > Stay awake when charging

Burn in: There should be a huge red flag anyone turning this on. It says "while charging" not "while debugging". Which means that even if your phone is plugged in to the mains it will keep on. This is especially bad if you have an AMOLED screen (e.g. Galaxy S series) which burns stuff in. I had this option on for a few weeks and now I have a permanent portrait status bar...

Notifications: Even if you use low brightness and don't forget to turn your screen off every time you put the phone down some apps wake your screen up if you get just a simple notification which leads to keeping it on most of the time while plugged in.

Security: If you just leave your screen on while charging and you're needed quickly for something at work, the first thing won't be "Ah, let me lock my phone first" and you may expose your dirty secrets if you accidentally left it on. Let me note that if you're working in an environment where you can't trust your collegaues, I would reconsider that employment.

Settings > Display > Screen Timeout

This is very risky if you have lot of apps that give you notifications. Especially if you have some spammers (Facebook, Family Guy or even GMail if you get a lot of mails).

Burn in: The risk is high with this as well. Now you don't even restrict it to "being on the wire" so it will just stay on whenever you forget to turn it off explicity or get a notification.

Battery drain: If you get one the screen will be on for the specified amount of time, draining your battery. And it will be on because sometimes you forget to turn it off, or just get a notification.

Hotpocket: if you get a notification while the phone is in your pocket the illumination from the screen and the confined space will heat your pockets and you may even get burned.

Pocket dial: if your screen turns on while the phone is in your pocket the risk of a pocket dial with increase with every second. Nowadays this is less likely though, because the Phone app is usually well hidden, but my pocket likes to change the date very often or read my emails.

Security: imagine you're in a public place and your phone is on the table, your friends will most likely abuse your unlocked screen if you turn around for long enough to talk to someone or take a quick break. Especially if they're inebriated. Obviously shorter timeouts decrease this risk.

Using a wake-lock

Permission: You'll need to add a possibly unnecessary android.permission.WAKE_LOCK permission to your app (luckily should be easy to add for only debug variants thanks to Manifest Merger in the Gradle Plugin).

Coding: You'll also need to manage releasing the lock yourself and maybe need to create a Service just for this. I'd like to note here that I've yet to use this feature in Android.

Useless: It also doesn't really help to keep the screen on, since it only keeps the CPU awake. See documentation.

FLAG_KEEP_SCREEN_ON while a Debugger is attached

If you read the documentation for this one, you'll find a very close approximation to your problem.

Coding: to only downside I can think of here is that you need to modify some code, but it's extremely simple (assuming you have a BaseActivity that all your other activities extend):

@Override protected void onResume() {
    super.onResume();
    if (BuildConfig.DEBUG) { // don't even consider it otherwise
        if (Debug.isDebuggerConnected()) {
            Log.d("SCREEN", "Keeping screen on for debugging, detach debugger and force an onResume to turn it off.");
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            Log.d("SCREEN", "Keeping screen on for debugging is now deactivated.");
        }
    }
}

To use it all you need to do is to attach a debugger, then leave the app (Home/Tasks button or turn the screen off) and then come back to it to trigger onResume.

Depending on what you usually debug, it may worth to put the above into onCreate so that it registers earlier and hopefully keeps the screen awake while debugging activity lifecycle before onResume.

In general I advise to use Run instead of Debug when trying out code and only attach the debugger when you found something: this will make your app tenfolds faster and this option the best there is.


Try this in the adb shell, although it seems not to work on some devices:

svc power stayon usb

On console

 while true; do adb shell input keyevent mouse ; sleep 1 ; done