问题
I want my android device to go to sleep (screen off), has someone an idea how to do it?
In the past I was using the
PowerManager
functiongoToSleep(time)
, but apparently this function does not exist anymore, I don't understand why.Other topics talk about those lines:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My wakelook"); wakeLock.acquire();
But it only allows the device to enter sleep mode, without telling him to go to sleep mode now.
I already tried the solution which consists of diminishing the luminosity of the screen, but it is not a real solution since the device doesn't really enter sleep mode. And I don't want to use the following code:
android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, timeoutInactivity);
because I don't want to touch the screen display settings and it is more a workaround than a solution.
PS : It's for a system app, so I don't have any right/permission limitation.
回答1:
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
public void sleep(){
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
mWakeLock.acquire();
}
Try this
来源:https://stackoverflow.com/questions/34285351/android-going-to-sleep-standby