Turning screen on and off programmatically not working on some devices

好久不见. 提交于 2019-12-23 01:45:16

问题


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);

来源:https://stackoverflow.com/questions/13416563/turning-screen-on-and-off-programmatically-not-working-on-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!