简体   繁体   中英

Is there a way to limit the time the screen is on with FLAG_KEEP_SCREEN_ON on Android?

I don't want to use a WakeLock. Is there a simple way to limit the time the screen is on using FLAG_KEEP_SCREEN_ON

If you read the docs here : docs

than you see that you don't need to take care about this. But you can, see:

Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags(): getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

You could use this in conjunction with a Runnable , post delayed on the Handler, which is the Android way to go, or with a TimerTask, which would be the more Java way.

Example:

    final long FIVE_MINUTES = 1000*60*5;
    Handler handler = new Handler();

    final Runnable r = new Runnable() {
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    };

    handler.postDelayed(r, FIVE_MINUTES);

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM