content: package com.dev.kit.basemodule.util;
  
  import android.app.Activity;
  import android.graphics.Color;
  import android.os.Build;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  /**
   * MIUI 工具类
   * Created by cy on 2017/10/18.
   */
  
  public class MIUIHelper {
      /**
       * 需要MIUIV6以上
       *
       * @param darkMode 是否把状态栏字体及图标颜色设置为深色
       * @return boolean 成功执行返回true
       */
      public static boolean MIUISetStatusBarLightMode(Activity activity, boolean darkMode) {
          String device = Build.MANUFACTURER;
          if (!device.equals("Xiaomi")) {
              return false;
          }
          Window window = activity.getWindow();
          if (window != null) {
              Class<? extends Window> clazz = activity.getWindow().getClass();
              try {
                  int darkModeFlag;
                  Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                  Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                  darkModeFlag = field.getInt(layoutParams);
                  Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                  extraFlagField.invoke(activity.getWindow(), darkMode ? darkModeFlag : 0, darkModeFlag);
                  // 以下设置可在style中完成<item name="android:windowLightStatusBar">true</item>
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                  }
                  return true;
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return false;
      }
  
      public static boolean setStatusBarLightMode(Activity activity, boolean darkMode) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              Window window = activity.getWindow();
              if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                  if (darkMode) {
                      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                  } else {
                      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                  }
              }
              return true;
          } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && "Xiaomi".equals(Build.MANUFACTURER)) {
              Class<? extends Window> clazz = activity.getWindow().getClass();
              try {
                  int darkModeFlag = 0;
                  Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                  Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                  darkModeFlag = field.getInt(layoutParams);
                  Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                  extraFlagField.invoke(activity.getWindow(), darkMode ? darkModeFlag : 0, darkModeFlag);
                  activity.getWindow().setStatusBarColor(Color.parseColor("#FFFFFF"));
                  return true;
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return false;
      }
  }
  