Organizational Research By

Surprising Reserch Topic

turn off screen on android


turn off screen on android  using -'android,wakelock,android-wake-lock'

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. So I think this isn't right.

Any hints would be helpful!
Thanks,

Mike
    

asked Sep 21, 2015 by badhwar.rohit
0 votes
24 views



Related Hot Questions

4 Answers

0 votes

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");
wl.acquire();
wl.release();

You will probably need this permission too:


UPDATE:

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;
getWindow().setAttributes(params);
answered Sep 21, 2015 by shegokar.anjeet
0 votes

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:

BroadcastReceiver

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

@Override
public void onReceive(Context arg0, Intent intent) {


    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        mBroadCastListenerCallBack.broadCastListenerCallBack__ScreenOff_onResponse();
    }       
}

}

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);
    filter.addAction(Intent.ACTION_SCREEN_OFF);

    BroadCastListenerCallBackItf broadCastListenerCallBack = this;

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



    //set Development --> disable STAY_ON_WHILE_PLUGGED_IN
    Settings.System.putInt(
            m_context.getContentResolver(), 
            Settings.System.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.putInt(m_context.getContentResolver(), 
            Settings.System.SCREEN_OFF_TIMEOUT, 15000);

    // wait 200 sec till get response from BroadcastReceiver on Screen Off
    mSync.doWait(wait*1000);


    // set previous settings
    Settings.System.putInt(m_context.getContentResolver(), 
            Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);

    // switch back previous state
    Settings.System.putInt(
            m_context.getContentResolver(), 
            Settings.System.STAY_ON_WHILE_PLUGGED_IN,
            BatteryManager.BATTERY_PLUGGED_USB);


    m_context.unregisterReceiver(mReceiver);


}





public void broadCastListenerCallBack__ScreenOff_onResponse() {
    mSync.doNotify();
}

....

AndroidSynchronize class

public class AndroidSynchronize {

    public void doWait(long l){
        synchronized(this){
            try {
                this.wait(l);
            } catch(InterruptedException e) {
            }
        }
    }       

    public void doNotify() {
        synchronized(this) {
            this.notify();
        }
    }   

    public void doWait() {
        synchronized(this){
            try {
                this.wait();
            } catch(InterruptedException e) {
            }
        }
    }
}

[EDIT]

You need to register permission:

android.permission.WRITE_SETTINGS
answered Sep 21, 2015 by pradip.bhoge
0 votes

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

answered Sep 21, 2015 by patilkiran.101
0 votes

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:


answered Sep 21, 2015 by rajeevr642

...