赞
踩
想要了解亮屏和灭屏的知识首先要了解系统服务 --- PowerManager.java
一:关于系统服务 --- PowerManager.java
PowerManager.java就是管理我们电源方面的功能的,当然也包括我们屏幕的亮和熄灭。我们的应用程序就是通过这个系统服务来实现亮屏和熄屏的功能的。、
1:首先来看一下怎么判断屏幕是亮屏还是灭屏
有两种判断方法:
第一种方法:广播的方式
- public void onCreate(Bundle savedInstanceState) {
- final IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- filter.addAction(Intent.ACTION_SCREEN_ON);
- registerReceiver(mBatInfoReceiver, filter);
-
- }
-
-
- private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(final Context context, final Intent intent) {
- final String action = intent.getAction();
- if(Intent.ACTION_SCREEN_ON.equals(action)){
- Log.d("lyj", "-----------------screen is on...");
- }else if(Intent.ACTION_SCREEN_OFF.equals(action)){
- Log.d("lyj", "----------------- screen is off...");
-
- }
- }
-
- };
第二种方法:
- PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
- boolean screen = pm.isScreenOn();
- if(!screen){//如果灭屏
- //相关操作
- }
-
二:定时熄灭屏幕
首先要了解控制屏幕的方法就是PowerManager.WakeLock这个东西就是亮屏控制,这里说的亮屏,它定义了几种类型的亮屏。如下:
类型 | cpu | 屏幕 | 键盘 |
PARTIAL_WAKE_LOCK | on | off | off |
SCREEN_DIM_WAKE_LOCK | on | dim | off |
SCREEN_BRIGHT_WAKE_LOCK | on | Brigh | off |
FULL_WAKE_LOCK | on | Brigh | Brigh |
- PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
- PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");
-
- //屏幕会持续点亮
- wakeLock.acquire();
- //释放锁,屏幕熄灭。
- wl.release();
所以这样来看,定时灭屏只需要延时调用release()方法即可:
- PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
- PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");
- wakeLock.acquire();
- //然后定时
- mTimeHandler.postDelayed(new Runnable(){
- public void run(){
- wakeLock.release();//
- }
- }, 10*1000);//延时10秒灭屏
特别注意:acquire()和release()要成对调用,如果只申请了亮屏,没有释放,屏幕会一直处于亮屏状态。所以调用acquire()后一定要记得释放!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。