Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For me those methods didn't work. So I used other scenario (not trivial) to make my screen off.</p> <p>Android has 2 flags that responsible to be awake: </p> <ul> <li>Display --> Screen TimeOut </li> <li>Application --> Development --> <em>Stay awake while charging</em> check box.</li> </ul> <p>I used followed flow:</p> <ol> <li><p>1st of all save your previous configuration, for example screen timeout was 1 min and <em>Stay awake while charging</em> checked.</p></li> <li><p>After, I uncheck <em>Stay awake while charging</em> and set screen timeout to minimal time.</p></li> <li><p>I register to broadcast receiver service to get event from android that screen turned off.</p></li> <li><p>When I got event on screen off, I set previous configuration to default: screen timeout was 1 min and <em>Stay awake while charging</em> checked.</p></li> <li><p>Unregister receiver</p></li> </ol> <p>After 15 sec. device sleeps</p> <p>Here is snippets of code:</p> <p><strong>BroadcastReceiver</strong></p> <pre><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; } @Override public void onReceive(Context arg0, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { mBroadCastListenerCallBack.broadCastListenerCallBack__ScreenOff_onResponse(); } } } </code></pre> <p><strong>Interface used as callback</strong></p> <pre><code>public interface BroadCastListenerCallBackItf { public void broadCastListenerCallBack__ScreenOff_onResponse(); } </code></pre> <p><strong>2 methods from main class:</strong></p> <pre><code>.... 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 --&gt; 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(); } .... </code></pre> <p><strong>AndroidSynchronize class</strong></p> <pre><code>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) { } } } } </code></pre> <p><strong>[EDIT]</strong></p> <p>You need to register permission:</p> <pre><code>android.permission.WRITE_SETTINGS </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload