0

I am creating an android app which has functionality to sleep the device based on certain conditions and wake up based on some other conditions. Before API 21, there was a method powerManager.goToSleep() in PowerManager which used to do the trick. But, now the same method is not accessible anymore. Is there any other way to do that?

int defaultTurnOffTime = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 60000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defaultTurnOffTime);

Above mentioned code is something I have got so far. But, it doesn't seem to work for me. Please suggest if there is any way to achieve the functionality.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

1 Answers1

0

Have you tried using PowerManager.Wakelock ?

Acquires the wake lock with a timeout.

Ensures that the device is on at the level requested when the wake lock was created. The lock will be released after the given timeout expires.

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

there are 4 types of Flags

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