Android going to sleep (standby)

2019-08-05 16:26发布

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 function goToSleep(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条回答
Rolldiameter
2楼-- · 2019-08-05 16:59
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;

public void sleep(){
      mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
      mWakeLock.acquire();
}

Try this

查看更多
登录 后发表回答