天天看点

android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCKPowerManager

in androidmanifest.xml 加上权限:

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

方法一:

public class unlockactivity2 extends activity {

/** called when the activity is first created. */

@override

public void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.main);

getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on);

}

方法二:

public class unlockactivity extends activity {

wakelock m_wklk;

powermanager pm = (powermanager)getsystemservice(power_service);

m_wklk = pm.newwakelock(powermanager.screen_dim_wake_lock, "cn");

m_wklk.acquire(); //设置保持唤醒

protected void ondestroy() {

// todo auto-generated method stub

super.ondestroy();

m_wklk.release(); //解除保持唤醒

protected void onpause() {

super.onpause();

m_wklk.release();//解除保持唤醒

protected void onresume() {

super.onresume();

解释:

用到的类

主要是这两个参数: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

on*

off

on

dim

bright

一般要使程序运行过程中背景保持常亮,使用 screen_bright_wake_lock 就可以, screen_bright_wake_lock cpu:唤醒 屏幕背光:唤醒 键盘灯:关闭

第二个参数:

description

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.

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.

<a target="_blank" href="http://www.cnblogs.com/chengning/archive/2012/04/26/2472789.html">http://www.cnblogs.com/chengning/archive/2012/04/26/2472789.html</a>

<a target="_blank" href="http://mysuperbaby.iteye.com/blog/1454178">http://mysuperbaby.iteye.com/blog/1454178</a>

<a target="_blank" href="http://wenku.baidu.com/view/a9c0fa01de80d4d8d15a4f96.html">http://wenku.baidu.com/view/a9c0fa01de80d4d8d15a4f96.html</a>

<a target="_blank" href="http://blog.csdn.net/xieying15170814609/article/details/7723928">http://blog.csdn.net/xieying15170814609/article/details/7723928</a>

继续阅读