Android 屏幕常亮 背景常亮

本文主题:

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


先上代码:

注意需要加权限

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

/**
 * 
 * @author zhujianbin
 *
 */
public class Utils {
	private static WakeLock wl;
	 
	/**
	 * 保持屏幕唤醒状态(即背景灯不熄灭)
	 * @param on 是否唤醒
	 */
	public static void keepScreenOn(Context context, boolean on) {
		if (on) {
			PowerManager pm = (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.
Flag ValueCPUScreenKeyboard
PARTIAL_WAKE_LOCKOn*OffOff
SCREEN_DIM_WAKE_LOCKOnDimOff
SCREEN_BRIGHT_WAKE_LOCKOnBrightOff
FULL_WAKE_LOCKOnBrightBright

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

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 ValueDescription
ACQUIRE_CAUSES_WAKEUPNormal 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_RELEASEIf 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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值