天天看點

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>

繼續閱讀