Created
October 21, 2022 01:25
-
-
Save akingdom/4b73701e68b7ce78ad440d0ca901eb13 to your computer and use it in GitHub Desktop.
How to programmatically control whether the screen may or may not turn off.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Android Java | |
// How to programmatically control whether the screen may or may not turn off. | |
// | |
// By Andrew Kingdom | |
// MIT license | |
// | |
package com.example.sample; | |
import android.os.Bundle; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.WindowManager; | |
public class MainActivity extends AppCompatActivity { | |
private boolean getFlagScreenOn() { | |
boolean value = (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0; | |
return value; | |
} | |
private void setFlagKeepScreenOn(boolean value) { | |
getWindow().setFlags( | |
value ? WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON : 0, | |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment