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)) {
                // 屏幕灭
            }
        }
    };