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

简介: In AndroidManifest.xml 加上权限:<uses-permission android:name="android.permission.WAKE_LOCK" /> 方法一:public class UnLockActivity2 extends Activity { /** Called when the activity is first creat



In AndroidManifest.xml 加上权限:

 

方法一:

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 {
/** Called when the activity is first created. */

WakeLock m_wklk;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
m_wklk = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "cn");

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

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
m_wklk.release(); //解除保持唤醒
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

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

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();


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


}

 

解释:

用到的类

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.


 


http://mysuperbaby.iteye.com/blog/1454178

http://wenku.baidu.com/view/a9c0fa01de80d4d8d15a4f96.html

http://blog.csdn.net/xieying15170814609/article/details/7723928

目录
相关文章
|
编解码
OTT与IPTV的区别是什么?
OTT与IPTV的区别是什么?
1806 0
|
缓存 开发工具 数据库
Docker 及 Docker Compose 安装指南
Docker 是一个开源的容器化平台,可以帮助我们快速构建、打包和运行应用程序。而 Docker Compose 则是用于管理多个容器应用的工具,可以轻松定义和管理多个容器之间的关系。现在,让我们开始安装过程吧!
1534 0
|
10月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
794 2
|
监控 API 数据库
什么是API?
API是应用程序编程接口(Application Programming Interface)的缩写,它定义了软件组件之间如何相互通信。API充当不同软件间的桥梁,允许应用程序使用另一个应用程序的功能或数据。
2561 4
|
IDE 物联网 开发工具
自学esb32烧录进军物联网和嵌入式
自学esb32烧录进军物联网和嵌入式
1032 18
|
数据可视化 Python
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
|
Web App开发 Java Linux
【性能优化】使用Perfetto定位应用启动性能的瓶颈
本篇文章将会结合我个人对Perfetto的实际使用经历,讲解车载应用的启动时间是如何测量得到的,测量出启动时间后,我们又该如何找出其中的性能瓶颈。
3532 1
【性能优化】使用Perfetto定位应用启动性能的瓶颈
|
Ubuntu 安全 测试技术
如何在Ubuntu 14.04上安装和配置Postfix作为仅发送的SMTP服务器
如何在Ubuntu 14.04上安装和配置Postfix作为仅发送的SMTP服务器
527 0
|
架构师 Java 数据安全/隐私保护
Fiddler安装和使用
Fiddler安装和使用
870 0