• Android 监听屏幕锁屏,用户解锁


    在做一个程序的时候,需要时刻保持某一服务是启动的,因此想到了通过监听屏幕SCREEN_ON和SCREEN_OFF这两个action。奇怪的是,这两个action只能通过代码的形式注册,才能被监听到,使用AndroidManifest.xml 完全监听不到。查了一下,发现这是PowerManager那边在发这个广播的时候,做了限制,限制只能有register到代码中的receiver才能接收。

    代码如下:

    public class ScreenListener {
        private Context mContext;
        private ScreenBroadcastReceiver mScreenReceiver;
        private ScreenStateListener mScreenStateListener;
    
        public ScreenListener(Context context) {
            mContext = context;
            mScreenReceiver = new ScreenBroadcastReceiver();
        }
    
        /**
         * screen状态广播接收者
         */
        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();
                }
            }
        }
    
        /**
         * 开始监听screen状态
         * 
         * @param listener
         */
        public void begin(ScreenStateListener listener) {
            mScreenStateListener = listener;
            registerListener();
            getScreenState();
        }
    
        /**
         * 获取screen状态
         */
        private void getScreenState() {
            PowerManager manager = (PowerManager) mContext
                    .getSystemService(Context.POWER_SERVICE);
            if (manager.isScreenOn()) {
                if (mScreenStateListener != null) {
                    mScreenStateListener.onScreenOn();
                }
            } else {
                if (mScreenStateListener != null) {
                    mScreenStateListener.onScreenOff();
                }
            }
        }
    
        /**
         * 停止screen状态监听
         */
        public void unregisterListener() {
            mContext.unregisterReceiver(mScreenReceiver);
        }
    
        /**
         * 启动screen状态广播接收器
         */
        private void registerListener() {
            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);
        }
    
        public interface ScreenStateListener {// 返回给调用者屏幕状态信息
            public void onScreenOn();
    
            public void onScreenOff();
    
            public void onUserPresent();
        }
    }

    在上面的代码中,用到了动态注册广播,在需要监听屏幕状态的activity中,当不需要监听的时候,如activity finish的时候,使用unregisterListener解除广播监听; 
    下面是在Activity中调用;

    ScreenListener l = new ScreenListener(this);
            l.begin(new ScreenStateListener() {
    
                @Override
                public void onUserPresent() {
                    Log.e("onUserPresent", "onUserPresent");
                }
    
                @Override
                public void onScreenOn() {
                    Log.e("onScreenOn", "onScreenOn");
                }
    
                @Override
                public void onScreenOff() {
                    Log.e("onScreenOff", "onScreenOff");
                }
            });
    
    通过以上步骤即实现了对屏幕状态的监听.


  • 相关阅读:
    洛谷 P1087 【FBI树】题解
    CF584D 【Dima and Lisa】题解
    洛谷 P1042 【乒乓球】题解
    洛谷 P2010 【回文日期】题解
    洛谷 P1233 【木棍加工】题解
    STM32嵌入式开发学习笔记(五):中断
    STM32嵌入式开发学习笔记(四):使用滴答计时器实现精准计时
    STM32嵌入式开发学习笔记(三):使用按键控制小灯
    STM32嵌入式开发学习笔记(二):将功能封装为库文件
    STM32嵌入式开发学习笔记(一)
  • 原文地址:https://www.cnblogs.com/zyw-205520/p/4754818.html
Copyright © 2020-2023  润新知