I use the code below to turn the screen on and off. Both pieces of code work on most devices (tested on Galaxy Note, Galaxy S2, etc. etc.) but don't work on a few (mostly tablets but some phones as well). On some devices screen on works but screen off doesn't, and the opposite holds for some other devices. I'm not sure what the cause is since it's pretty standard code (and since it works on some devices that means triggering these functions i.e. the function call, is not a problem). Are there alternate ways of turning the screen on/off that I could use? Any help is greatly appreciated.
Screen On:-
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
|PowerManager.ACQUIRE_CAUSES_WAKEUP
|PowerManager.ON_AFTER_RELEASE, "");
wl.acquire();
Screen Off (This calls an activity that simply uses the device administrator privileges to lock and turn of the screen. On some devices, this only locks the screen. On others, it doesn't do anything. On some, it works perfectly):-
devicePolicyManager.lockNow();
Here's another method I'm trying that I can't get to work too well. Basically I set the brightness to 0 and that turns the screen off and then the activity performing these actions calls finish() but for some reason I can't turn my screen back on till 5-10 seconds after this, no matter what buttons I keep pressing (Home, Power).
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);