本文主题:
使android程序运行过程中,屏幕背景灯保持唤醒,即不黑屏。
先上代码:
注意需要加权限
[html]view plain copy<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
[java]view plain copy/** * *@authorzhujianbin * */ publicclassUtils{ privatestaticWakeLockwl; /** *保持屏幕唤醒状态(即背景灯不熄灭) *@paramon是否唤醒 */ publicstaticvoidkeepScreenOn(Contextcontext,booleanon){ if(on){ PowerManagerpm=(PowerManager)context.getSystemService(Context.POWER_SERVICE); wl=pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,"==KeepScreenOn=="); wl.acquire(); }else{ wl.release(); wl=null; } } }
解释:
用到的类
PowerManager
主要是这两个参数:PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE
下面是 android 官方API解释:
he following flags are defined, with varying effects on system power. These flags are mutually exclusive - you may only specify one of them.
一般要使程序运行过程中背景保持常亮,使用
SCREEN_BRIGHT_WAKE_LOCK 就可以,
SCREEN_BRIGHT_WAKE_LOCKCPU:唤醒 屏幕背光:唤醒 键盘灯:关闭
第二个参数:
In addition, you can add two more flags, which affect behavior of the screen only.These flags have no effect when combined with aPARTIAL_WAKE_LOCK
.
如果觉得《Android 屏幕常亮》对你有帮助,请点赞、收藏,并留下你的观点哦!