On Android, you can programmatically keep a specific display (such as the screen) turned on using either DisplayManager (for managing display states) or PowerManager (for controlling wake locks). Here’s how you can achieve this using the Android API:
DisplayManager (API 17+)This is the recommended way to manage display states, such as keeping a specific display on.
DisplayManager system service.DisplayManager.setDisplayState() to set the state of a specific display.javaimport android.hardware.display.DisplayManager; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayManager displayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE); if (displayManager != null) { // Get the default display Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY); if (display != null) { // Set the display state to ON displayManager.setDisplayState(display.getDisplayId(), Display.STATE_ON); } } } }
android.permission.DISPLAY in your manifest.PowerManager Wake Lock (Legacy Approach)If you want to keep the device screen on regardless of user activity, you can use a wake lock.
PARTIAL_WAKE_LOCK or SCREEN_DIM_WAKE_LOCK.javaimport android.os.PowerManager; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private PowerManager.WakeLock wakeLock; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); if (powerManager != null) { // Acquire a wake lock to keep the screen on wakeLock = powerManager.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyApp::WakeLock" ); wakeLock.acquire(); } } @Override protected void onDestroy() { super.onDestroy(); if (wakeLock != null && wakeLock.isHeld()) { wakeLock.release(); } } }
android.permission.WAKE_LOCK in your manifest.SCREEN_DIM_WAKE_LOCK keeps the screen dimmed but on.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ONFor keeping the screen on in a specific activity, you can set a flag in the window.
javaimport android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Keep the screen on getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } @Override protected void onDestroy() { super.onDestroy(); // Clear the flag when the activity is destroyed getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }
DisplayManagerandroid.permission.DISPLAYPowerManager Wake Lockandroid.permission.WAKE_LOCKFLAG_KEEP_SCREEN_ONWhich method should you use?
DisplayManager.FLAG_KEEP_SCREEN_ON or PowerManager.