Android锁屏与解锁广播监听

Android锁屏与解锁广播监听

在Android开发中经常会碰到锁屏和解锁的需求,这里封装一个ScreenListener统一管理广播的注册和反注册,同时设置锁屏和解锁的回调监听。

代码如下:

    public class ScreenListener {
        private Context mContext;
        private ScreenBroadcastReceiver receiver;
        private ScreenStateListener mScreenStateListener;

        public ScreenListener(Context context) {
            mContext = context;
            receiver = new ScreenBroadcastReceiver();
        }

        public void register(ScreenStateListener screenStateListener) {
            if (screenStateListener != null) {
                mScreenStateListener = screenStateListener;
            }
            if (receiver != null) {
                IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                filter.addAction(Intent.ACTION_SCREEN_ON);
                filter.addAction(Intent.ACTION_USER_PRESENT);
                mContext.registerReceiver(receiver, filter);
            }
        }

        public void unregister() {
            if (receiver != null) {
                mContext.unregisterReceiver(receiver);
            }
        }


        private class ScreenBroadcastReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                    String action = intent.getAction();
                    if (Intent.ACTION_SCREEN_ON.equals(action)) {
                        if (mScreenStateListener != null) {
                            Log.e("zhang", "ScreenBroadcastReceiver --> ACTION_SCREEN_ON");
                            mScreenStateListener.onScreenOn();
                        }
                    } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                        if (mScreenStateListener != null) {
                            Log.e("zhang", "ScreenBroadcastReceiver --> ACTION_SCREEN_OFF");
                            mScreenStateListener.onScreenOff();
                        }
                    } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                        if (mScreenStateListener != null) {
                            Log.e("zhang", "ScreenBroadcastReceiver --> ACTION_USER_PRESENT");
                            mScreenStateListener.onUserPresent();
                        }
                    }
                }
            }
        }

        public interface ScreenStateListener {
            void onScreenOn();

            void onScreenOff();

            void onUserPresent();
        }
    }

如果有需要,可以采用单例的写法


MainActivity中的调用

    public class MainActivity extends AppCompatActivity {

        private ScreenListener listener;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listener = new ScreenListener(this);
            listener.register(new ScreenListener.ScreenStateListener() {
                @Override
                public void onScreenOn() {
                    Log.e("zhang", "MainActivity --> onScreenOn--> ");
                }

                @Override
                public void onScreenOff() {
                    Log.e("zhang", "MainActivity --> onScreenOff--> ");
                }

                @Override
                public void onUserPresent() {
                    Log.e("zhang", "MainActivity --> onUserPresent--> ");
                }
            });
        }

        @Override
        protected void onDestroy() {
            if (listener != null) {
                listener.unregister();
            }
            super.onDestroy();
        }
    }

打印的log结果:

    zhang: ScreenBroadcastReceiver --> ACTION_SCREEN_OFF
    zhang: MainActivity --> onScreenOff--> 
    zhang: ScreenBroadcastReceiver --> ACTION_SCREEN_ON
    zhang: MainActivity --> onScreenOn--> 
    zhang: ScreenBroadcastReceiver --> ACTION_USER_PRESENT
    zhang: MainActivity --> onUserPresent-->

关闭菜单