Turn off screen on Android

The question:

I am trying to turn on and off the display after a certain action happens (Lets just worry about turning the screen off for now). From what I understand from wake lock, this is what I have:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);    
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

When I read other posts on stackoverflow and else where, they seem to tell me that PARTIAL_WAKE_LOCK will turn the screen off. But if I read the SDK it says that it will only allow the screen to be turned off. I think this isn’t right.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

There are two choices for turning the screen off:

PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);

// Choice 1
manager.goToSleep(int amountOfTime);

// Choice 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");

You will probably need this permission too:

<uses-permission android:name="android.permission.WAKE_LOCK" />


Try this method; android turns off the screen once the light level is low enough.

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

Method 2

The following is copied from SDK document.
If you want to keep screen on, I think SCREEN_BRIGHT_WAKE_LOCK is enough.

Flag Value                CPU   Screen  Keyboard

PARTIAL_WAKE_LOCK          On*    Off      Off

SCREEN_DIM_WAKE_LOCK       On     Dim      Off

SCREEN_BRIGHT_WAKE_LOCK    On     Bright   Off

FULL_WAKE_LOCK             On     Bright   Bright

Method 3

For me those methods didn’t work. So I used other scenario (not trivial) to make my screen off.

Android has 2 flags that responsible to be awake:

  • Display –> Screen TimeOut
  • Application –> Development –> Stay awake while charging check box.

I used followed flow:

  1. 1st of all save your previous configuration, for example screen timeout was 1 min and Stay awake while charging checked.

  2. After, I uncheck Stay awake while charging and set screen timeout to minimal time.

  3. I register to broadcast receiver service to get event from android that screen turned off.

  4. When I got event on screen off, I set previous configuration to default: screen timeout was 1 min and Stay awake while charging checked.

  5. Unregister receiver

After 15 sec. device sleeps

Here is snippets of code:


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

 * Catch Screen On/Off
 * */
public class BroadcastReceiverScreenListener extends BroadcastReceiver{

private BroadCastListenerCallBackItf mBroadCastListenerCallBack = null;

public BroadcastReceiverScreenListener(
        BroadCastListenerCallBackItf broadCastListenerCallBack) {
    this.mBroadCastListenerCallBack = broadCastListenerCallBack;

public void onReceive(Context arg0, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {


Interface used as callback

public interface BroadCastListenerCallBackItf {
    public void broadCastListenerCallBack__ScreenOff_onResponse();

2 methods from main class:


AndroidSynchronize mSync = new AndroidSynchronize();


public void turnScreenOff(int wait){
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

    BroadCastListenerCallBackItf broadCastListenerCallBack = this;

    BroadcastReceiver mReceiver = new BroadcastReceiverScreenListener(broadCastListenerCallBack);       
    m_context.registerReceiver(mReceiver, filter);

    //set Development --> disable STAY_ON_WHILE_PLUGGED_IN
            0                                );

    // take current screen off time 
    int defTimeOut = Settings.System.getInt(m_context.getContentResolver(), 
            Settings.System.SCREEN_OFF_TIMEOUT, 3000);
    // set 15 sec
            Settings.System.SCREEN_OFF_TIMEOUT, 15000);

    // wait 200 sec till get response from BroadcastReceiver on Screen Off

    // set previous settings
            Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);

    // switch back previous state



public void broadCastListenerCallBack__ScreenOff_onResponse() {


AndroidSynchronize class

public class AndroidSynchronize {

    public void doWait(long l){
            try {
            } catch(InterruptedException e) {

    public void doNotify() {
        synchronized(this) {

    public void doWait() {
            try {
            } catch(InterruptedException e) {


You need to register permission:


Method 4

Per this link, You can also turn the screen off like this:

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

1000 is in milliseconds which means 1 second, you can replace it with any value as desired.

Needed permission:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Method 5

try –
wakeLock.acquire(1000); // specify the time , it dims out and eventually turns off.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment