content: package tv.baokan.baokanandroid.utils;
  
  import android.app.Activity;
  import android.view.Window;
  import android.view.WindowManager;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  public class StatusUtils {
  
      /**
       * 设置MIUI状态栏模式
       *
       * @param activity activity
       * @param darkmode 是否是黑暗系
       * @return 是否设置成功
       */
      public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
          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);
              return true;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return false;
      }
  
      /**
       * 设置魅族状态栏模式
       *
       * @param activity activity
       * @param darkmode 是否是黑暗系
       * @return 是否设置成功
       */
      public static boolean setMeizuStatusBarDarkMode(Activity activity, boolean darkmode) {
          boolean result = false;
          if (activity != null) {
              try {
                  WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                  Field darkFlag = WindowManager.LayoutParams.class
                          .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                  Field meizuFlags = WindowManager.LayoutParams.class
                          .getDeclaredField("meizuFlags");
                  darkFlag.setAccessible(true);
                  meizuFlags.setAccessible(true);
                  int bit = darkFlag.getInt(null);
                  int value = meizuFlags.getInt(lp);
                  if (darkmode) {
                      value |= bit;
                  } else {
                      value &= ~bit;
                  }
                  meizuFlags.setInt(lp, value);
                  activity.getWindow().setAttributes(lp);
                  result = true;
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return result;
      }
  
  }
  