Put Android on standby by program

3

I want the application to pass the device to standby as when pressing the on / off key, but I can not find how to do it.

    
asked by Aniceto Aniceto Pérez 03.05.2017 в 20:37
source

1 answer

2

This can not be done by any application, it must be a system application so that it has access to be able to perform the action of sending the device to Standby.

For this you can use the class PowerManager but for this you need permission:

<uses-permission android:name="android.permission.DEVICE_POWER"/>

which is only allowed for system applications.

With this method you would achieve what you want:

   private  void toStandBy(){

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Standby");
        wl.acquire();
        wl.release();
    }
    
answered by 03.05.2017 в 21:36