0

I'm building an Android application, and I need the screen of the mobile device to be awake for ten minutes. How can it be done?

I think it could be some timer, but there is another question then; how to set screen off programmatically?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
ToRaRo
  • 611
  • 1
  • 5
  • 7

1 Answers1

2

That is the old way how manage. Now, try to use this one:

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

and after 10 min. you should disable usin or :

     this.getWindow().setFlags(this.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

or :

      this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

You can calculate the timer using this peace of code:

   final Handler mTimerHandler = new Handler();
   final Handler threadHandler = new Handler();
   new Thread() {
           @Override
           public void run() {
                   threadHandler.postDelayed(new Runnable() {
                           public void run() {

                           // HERE THE CODE TO DISABLE THE FLAG

                           }
                   }, 10000);   //10 seconds
           }
   }.start();

if you want 10 min = 10 x 60 seconds x 1000 milisecods = 600 000 It is the value that you have to set up.

Edit: the solutions with WakeLock object are not recommended for Android anymore.

Juan Pedro Martinez
  • 1,924
  • 1
  • 15
  • 24