Android:FLAG_KEEP_SCREEN_ON

FLAG_KEEP_SCREEN_ON is the best way to keep the screen on and off, so how should this method be used?

I found the following code on stackoverflow:
    @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
Simply put getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); add it to onCreate, and use WindowManager to manage the window.

Since it is added to the onCreate of the Activity, you can know that it must be for the current Activity without testing. What if you want to keep all activities awake?

In fact, if you use the trick of BaseActivity I mentioned before, it can be easily solved:

add it to the onCreate of BaseActivity and, if any, BaseTabActivity, BaseMapActivity, etc.:
getWindow().addFlags(WindowManager.LayoutParams. FLAG_KEEP_SCREEN_ON);
to ensure that the Activity that inherits them all inherits to keep the screen awake.
 There are many situations in which the screen of the phone should be kept on, such as when playing a video.
KEEP_SCREEN_ON_FLAG locks are managed uniformly by WindowsMaganerService
mHoldingScreenWakeLock = pmc.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,""KEEP_SCREEN_ON_FLAG"");
Generally, for the upper layer, just add the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag to the current window getWindow
().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

The android video player MediaPlayer has a unified interface to set flags.
Landroid/media/MediaPlayer;->setScreenOnWhilePlaying(Z)V

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691399&siteId=291194637