Android OS2.2 used to have an option under Settings/Applications/Development to disable screen lock during USB debugging. After upgrading my Samsung Galaxy S to OS2.3.3 this
Improving best answer:
Debug.isDebuggerConnected() is not enough if you are not debugging right now, but still working with an app while device is connected via ADB to Android studio. Then we have to add ADB Enabled check.
Settings.Global.getInt(contentResolver, Settings.Global.ADB_ENABLED, 0) == 1 where 1 is when ADB is enabled
@Override protected void onResume() {
super.onResume();
if (BuildConfig.DEBUG) { // don't even consider it otherwise
if (Debug.isDebuggerConnected() ||
Settings.Global.getInt(getContentResolver(), Settings.Global.ADB_ENABLED, 0) == 1) {
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.");
}
}
}