Skip to content

Instantly share code, notes, and snippets.

@akingdom
Created October 21, 2022 01:25
Show Gist options
  • Save akingdom/4b73701e68b7ce78ad440d0ca901eb13 to your computer and use it in GitHub Desktop.
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.
// 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