本文主题:

使android程序运行过程中,屏幕背景灯保持唤醒,即不黑屏。


先上代码:

注意需要加权限

[html] view plain copy
  1. <uses-permissionandroid:name="android.permission.WAKE_LOCK"/>

[java] view plain copy
  1. /**
  2. *
  3. *@authorzhujianbin
  4. *
  5. */
  6. publicclassUtils{
  7. privatestaticWakeLockwl;
  8. /**
  9. *保持屏幕唤醒状态(即背景灯不熄灭)
  10. *@paramon是否唤醒
  11. */
  12. publicstaticvoidkeepScreenOn(Contextcontext,booleanon){
  13. if(on){
  14. PowerManagerpm=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
  15. wl=pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,"==KeepScreenOn==");
  16. wl.acquire();
  17. }else{
  18. wl.release();
  19. wl=null;
  20. }
  21. }
  22. }


解释:

用到的类

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.
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright

一般要使程序运行过程中背景保持常亮,使用

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.

Flag Value Description
ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

更多相关文章

  1. 设置屏幕默认横屏
  2. Android之SharedPreferences对参数数据的存取
  3. android 获取Service(服务)的运行状态
  4. android中View坐标学习
  5. 【Appium + Python3】之安卓8.1,使用xpath定位不到元素
  6. Android(安卓)8.0 悬浮窗变动与用法
  7. Android二进制文件转码到Base64并通过Post进行提交
  8. Android第十二课 jni函数的静态绑定
  9. android java js 回调 真心好用

随机推荐

  1. android CTS SELinuxDomainTest# testIni
  2. Android(安卓)getResources的作用和须要
  3. androidのview游戏框架
  4. Android学习之ListView使用基础
  5. 【CMake】CMake 引入 ( Android(安卓)NDK
  6. Android如何使用注解进行代码检查
  7. Android下使用activation发送邮件
  8. DSL element ‘android.dataBinding.enab
  9. 解决Flutter 编译异常path_provider
  10. Android App开发基础篇—数据存储(SP和文