Seung-Chan Ahn Seung-Chan Ahn -4 years ago 99
Android Question

android: turn screen Off

Purpose :

in an android app, turn screen off when user touch a button.

Answers already exist :

  1. Set screen brightness to zero.

    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
    params.screenBrightness = 0;

    This code makes screen dim, but can't turn off.

  2. Use PowerManager.WakeLock

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
    //..screen will stay on during this section..

    This code keep a phone at 'Wake_State'. In following Table,
    'PARTIAL_WAKE_LOCK' looks to turn off screen. But it just allows that a user turn screen off personally (maybe with power-button).

So, I want to turn screen fully off not using 'power-button'. Thanks for reading.

--- Add about my intention -------------------------

My user needs to turn screen off while other apps are keep working. Not to make 'POWER_KEY_DOWN(UP)_EVENT' I chose this way. I welcome another way which archive same purpose.

Answer Source

You can achieve it by setting the wake_setting time to 1 second.

I have already tried both of above shared ways, which shows slightly visible screen on but by setting wake_setting to 1 sec.

I have to just turn off the screen. For that purpose you are going to add SETTING_Change permission ( only available in rooted handset).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download