With Android API how to keep only a given display turned on?
Apr 12, 10:57pm

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:


1. Using DisplayManager (API 17+)

This is the recommended way to manage display states, such as keeping a specific display on.

Steps:

  1. Check if the device supports multiple displays.
  2. Get the DisplayManager system service.
  3. Use DisplayManager.setDisplayState() to set the state of a specific display.

Example Code:

java
import 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);
            }
        }
    }
}

Notes:

  • Requires android.permission.DISPLAY in your manifest.
  • Works for external displays (e.g., HDMI, wireless displays).

2. Using PowerManager Wake Lock (Legacy Approach)

If you want to keep the device screen on regardless of user activity, you can use a wake lock.

Steps:

  1. Acquire a wake lock with PARTIAL_WAKE_LOCK or SCREEN_DIM_WAKE_LOCK.
  2. Release the wake lock when no longer needed.

Example Code:

java
import 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();
        }
    }
}

Notes:

  • Requires android.permission.WAKE_LOCK in your manifest.
  • SCREEN_DIM_WAKE_LOCK keeps the screen dimmed but on.
  • Always release the wake lock when done to avoid battery drain.

3. Using WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

For keeping the screen on in a specific activity, you can set a flag in the window.

Example Code:

java
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);

        // 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);
    }
}

Notes:

  • No additional permissions required.
  • Only affects the current activity.

Summary Table

DisplayManager
Control specific displays
android.permission.DISPLAY
17+
PowerManager Wake Lock
Keep screen on (legacy)
android.permission.WAKE_LOCK
All
FLAG_KEEP_SCREEN_ON
Keep screen on for activity
None
All

Which method should you use?

  • For modern Android (API 17+), use DisplayManager.
  • For legacy or simple cases, use FLAG_KEEP_SCREEN_ON or PowerManager.
Apr 12, 10:57pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.