Search code examples
androidsleepandroid-6.0-marshmallownexus-7

how to keep android display from sleeping or dimming while plugged in?


i have some nexus 7's (2013) that are not rooted and have api 6.0.1 with may security patches.

i have stay awake on in developer options. i have everything off under the display settings except for sleep (which can not be turned off and is set to 30 minutes). despite this, the tablet dims after about 30 minutes.

i tried acquiring and releasing wakelock in on resume and on pause with no joy.

adb shell settings put global stay_on_while_plugged_in 0 - does not work either (neither does a value of 7 - the original value was 3)


Solution

  • Here is the google documentation: https://developer.android.com/training/scheduling/wakelock.html

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }