Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically set screen to off in android

Tags:

android

can I programmatically set screen to off in android if is not done automatically after 1 minute of inactivity.

is this possible to do programmatically in android?

I found this threads: Android: How to turn screen on and off programmatically?

Turning screen on and off programmatically not working on some devices

but there is no timer after 1 minute.

like image 913
senzacionale Avatar asked Dec 04 '12 05:12

senzacionale


People also ask

How to KEEP screen awake in Android programmatically?

Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON . You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.

How do I turn my screen on Android?

Method #1: Enable the double-tap gesture to turn on the phone screen. One of the easiest and quickest ways to turn on your Android phone's screen without using the power button is by enabling the double-tap gesture in settings.


1 Answers

Yes, the method best used in this case is to programmatically set screen timeout instead.

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" />

UPDATE

It will overwrite the phone system value (Settings/Display/Sleep), so perhaps you need to restore the current settings after finish:

private static final int SCREEN_OFF_TIME_OUT = 13000;
private int mSystemScreenOffTimeOut;
private void setScreenOffTimeOut() {
    try {
        mSystemScreenOffTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, SCREEN_OFF_TIME_OUT);
    } catch (Exception e) {
        Utils.handleException(e);
    }
}

private void restoreScreenOffTimeOut() {
    if (mSystemScreenOffTimeOut == 0) return;
    try {
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, mSystemScreenOffTimeOut);
    } catch (Exception e) {
        Utils.handleException(e);
    }
}
like image 76
Royston Pinto Avatar answered Nov 10 '22 05:11

Royston Pinto