0

in one of my Activities I want to keep screen on for 2 minutes (e.g.). I know I can keep screen on with:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

but how to do that for an specific duration ?

Soheil
  • 1,676
  • 7
  • 34
  • 65

3 Answers3

2

You have many ways to clear flags after 2 minutes..like you can use timer or thread or handler

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }, 20000);

in this way you can clear the flags

Meenal
  • 2,879
  • 5
  • 19
  • 43
1

Take this:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // turn screen off function
        }
    }, 2000);

After 2sec it will turn off the screen. You just have to put the turn off function in it.

silvia_aut
  • 1,481
  • 6
  • 19
  • 33
0
 Handler handler = new Handler(); 
 // run a thread after 2 seconds to start the home screen
 handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                // start your screen  
            }
        }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
silvia_aut
  • 1,481
  • 6
  • 19
  • 33
jigspatel
  • 400
  • 3
  • 14