I've made a simple application to turn the screen on and off once a button is pressed, the code does not present any error but it doesn't do anything. Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("Wakelock")
@Override
public void onClick(View arg0) {
Log.d(TAG, String.valueOf(screenWakeLock));
if (screenWakeLock != null) {
if(screenWakeLock.isHeld())
Log.d(TAG, "wavelock held");
screenWakeLock.release();
screenWakeLock = null;
}else{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "screenWakeLock");
screenWakeLock.acquire();
Log.d(TAG, String.valueOf(screenWakeLock));
}
}
});
}
and the permissions in the manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.DEVICE_POWER"></uses-permission>
There's no error, but nothing happens on the screen. Does anyone know why?
Thank you in advance!
In another question:
Programmatically switching off Android phone
I found a way that works. After rooting you phone:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}