Android · 2016年8月2日 0

Android 监听锁屏/开屏事件

1. 两种方案

(1) 监听BroadcastReceiver

(2)获取PowerManager事件

2.  BroadcastReceiver

2.1. 事件

总共有3个事件:

Intent.ACTION_SCREEN_ON : 屏幕点亮
Intent.ACTION_SCREEN_OFF :屏幕关闭
Intent.ACTION_USER_PRESENT: 用户解锁

2.2. 权限

监听用户解锁需要在AndroidManifest中注册权限

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

2.3. IntentFilter代码

    if (mContext != null) {  
        IntentFilter filter = new IntentFilter();  
        filter.addAction(Intent.ACTION_SCREEN_ON);  
        filter.addAction(Intent.ACTION_SCREEN_OFF);  
        filter.addAction(Intent.ACTION_USER_PRESENT);  
        mContext.registerReceiver(mScreenReceiver, filter);  
    }

 

3. PowerManager

    PowerManager manager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);  
    if (manager.isScreenOn()) {  
        if (mScreenStateListener != null) {  
            mScreenStateListener.onScreenOn();  
        }  
    } else {  
        if (mScreenStateListener != null) {  
            mScreenStateListener.onScreenOff();  
        }  
    }

 

4. Code

    import android.annotation.SuppressLint;  
    import android.content.BroadcastReceiver;  
    import android.content.Context;  
    import android.content.Intent;  
    import android.content.IntentFilter;  
    import android.os.PowerManager;  
      
    /**  
     * Created by xiyanglu on 4/11/15.  
     */  
    public class ScreenObserver {  
        private Context mContext;  
        private ScreenBroadcastReceiver mScreenReceiver;  
        private ScreenStateListener mScreenStateListener;  
      
        public ScreenObserver(Context context) {  
            mContext = context;  
            mScreenReceiver = new ScreenBroadcastReceiver();  
        }  
      
        public void startObserver(ScreenStateListener listener) {  
            mScreenStateListener = listener;  
            registerListener();  
            getScreenState();  
        }  
      
        public void shutdownObserver() {  
            unregisterListener();  
        }  
      
        /**  
         * 获取screen状态  
         */  
        @SuppressLint("NewApi")  
        private void getScreenState() {  
            if (mContext == null) {  
                return;  
            }  
      
            PowerManager manager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);  
            if (manager.isScreenOn()) {  
                if (mScreenStateListener != null) {  
                    mScreenStateListener.onScreenOn();  
                }  
            } else {  
                if (mScreenStateListener != null) {  
                    mScreenStateListener.onScreenOff();  
                }  
            }  
        }  
      
        private void registerListener() {  
            if (mContext != null) {  
                IntentFilter filter = new IntentFilter();  
                filter.addAction(Intent.ACTION_SCREEN_ON);  
                filter.addAction(Intent.ACTION_SCREEN_OFF);  
                filter.addAction(Intent.ACTION_USER_PRESENT);  
                mContext.registerReceiver(mScreenReceiver, filter);  
            }  
        }  
      
        private void unregisterListener() {  
            if (mContext != null)  
                mContext.unregisterReceiver(mScreenReceiver);  
        }  
      
      
        private class ScreenBroadcastReceiver extends BroadcastReceiver {  
            private String action = null;  
     
            @Override  
            public void onReceive(Context context, Intent intent) {  
                action = intent.getAction();  
                if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏  
                    mScreenStateListener.onScreenOn();  
                } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏  
                    mScreenStateListener.onScreenOff();  
                } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁  
                    mScreenStateListener.onUserPresent();  
                }  
            }  
        }  
      
        public interface ScreenStateListener {// 返回给调用者屏幕状态信息  
            public void onScreenOn();  
      
            public void onScreenOff();  
      
            public void onUserPresent();  
        }  
    }

 

Android监听锁屏键、Home键

项目用到了,记录下,返回键什么的物理按键使用onKeyDown监听就可以了,但是锁屏键、Home键不可以,这里使用广播监听两者状态变化

final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

IntentFilter片段:

1
2
3
4
mFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//home键
    mFilter.addAction(Intent.ACTION_SCREEN_ON);  //开屏
    mFilter.addAction(Intent.ACTION_SCREEN_OFF);//锁屏
    mFilter.addAction(Intent.ACTION_USER_PRESENT);//解锁

onRecevier片段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
           if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
               String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
               if (reason != null) {
                   if (mListener != null) {
                       if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                           // 短按home键 
                           mListener.onHomePressed();
                       } else if (reason
                               .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                           // 长按home键 
                           mListener.onHomeLongPressed();
                       }
                   }
               }
           }
           if(action.equals(Intent.ACTION_SCREEN_ON)){ 
               mListener.onScreenOn(); 
           }else if(action.equals(Intent.ACTION_SCREEN_OFF)){ 
               mListener.onScreenOff(); 
           else{// 解锁
               //TODO
           }

最后提醒下,对于锁屏操作,由于先走的activity的onPause,onStop,所以不可以像以往反注册广播样在onPause里进行反注册,否则的话会接收不到锁屏操作

Share this: