I want to implement some code lines that allow Android turn on/off screen like we quick turn on/off device by using Power button. Is there any way to do that?
It seems you want to lock the device not turn it off. To lock the device its a simple code. Mentioned here
It uses device Policy Manager
In case of a RuntimeError use the method described in this question to set permissions
To switch off the device:
This is not possible but you can reboot it using PowerManager
Note this requires REBOOT permission and OS may cancel it.