Turn off/on android screen

*爱你&永不变心* 提交于 2019-12-25 17:35:17

问题


I am trying to turn the android screen off and then on again, after a few seconds. The "turn off" part works, with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;
layoutParam.screenBrightness = 0; 
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

But then, when I try to turn the screen on again, it does not work with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
layoutParam.screenBrightness = oldBrightness;
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

Any idea to solve that ?

thanks


回答1:


I think U can try Powermanager WakeLock maybe it will work. I m using this code in my application. and it works well. :)

Also u need to set permission in manifest.

<uses-permission android:name="android.permission.WAKE_LOCK"/> // Manifest Permission

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
                WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
                wakeLock.acquire();



回答2:


First off, are you sure about the "/255f" in this line:

oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;

What is the value in "oldBrightness" when you get/set it ?

Maybe you could try this:

PowerManager.WakeLock lck = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
lck.acquire();

The normal wake lock doesn't turn the screen on but causes it to stay on when a user causes it. But this flag forces the screen to turn on immediately. It requires the "android.permission.WAKE_LOCK".

More about it:

http://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK

And the screen properties (on, off, bright, dim, etc.):

http://developer.android.com/reference/android/os/PowerManager.html



来源:https://stackoverflow.com/questions/10469511/turn-off-on-android-screen

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