How do I keep my screen unlocked during USB debugging?

前端 未结 19 1349
清酒与你
清酒与你 2020-12-13 07:55

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

19条回答
  •  感动是毒
    2020-12-13 08:51

    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.");
            }
        }
    }
    

提交回复
热议问题