歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 屏幕常亮 背景常亮

本文主題:

使Android程序運行過程中,屏幕背景燈保持喚醒,即不黑屏。


先上代碼:

注意需要加權限

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

 
  1. /** 
  2.  *  
  3.  * @author zhujianbin 
  4.  * 
  5.  */  
  6. public class Utils {  
  7.     private static WakeLock wl;  
  8.        
  9.     /** 
  10.      * 保持屏幕喚醒狀態(即背景燈不熄滅) 
  11.      * @param on 是否喚醒 
  12.      */  
  13.     public static void keepScreenOn(Context context, boolean on) {  
  14.         if (on) {  
  15.             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);  
  16.             wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "==KeepScreenOn==");  
  17.             wl.acquire();  
  18.         }else {  
  19.             wl.release();  
  20.             wl = null;  
  21.         }  
  22.     }  
  23. }  

解釋:

用到的類

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_LOCK   CPU:喚醒  屏幕背光:喚醒  鍵盤燈:關閉

第二個參數:

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.
Copyright © Linux教程網 All Rights Reserved