Skip to content

Android 监听屏幕亮灭

使用广播监听设备屏幕亮灭状态。这个是系统发出来的广播。

使用的action

  • Intent.ACTION_SCREEN_ON 亮屏
  • Intent.ACTION_SCREEN_OFF 灭屏
    private void registerScreenListener() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mScreenReceiver, filter);
    }

    private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) {
                // 屏幕亮
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                // 屏幕灭
            }
        }
    };

作者: RustFisher
联系: rf.cs@foxmail.com
博客: rustfisher.com | RustFisher cnblog
示例: AndroidTutorial Gitee, Tutorial Github
链接: https://www.an.rustfisher.com/android/broadcast/screen-on-broadcast/
一家之言,仅当抛砖引玉。如有错漏,还请指出。