public abstract class BaseActivity extends AppCompatActivity {
private static boolean isMiUi = false;
protected void initWindow() {
}
protected abstract int getLayoutId();
protected abstract void initView();
protected abstract void initData();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initWindow();
setContentView(getLayoutId());
initView();
initData();
}
/**
* 设置小米黑色状态栏字体
*/
@SuppressLint("PrivateApi")
private void setMIUIStatusBarDarkMode() {
if (isMiUi) {
Class<? extends Window> clazz = 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(getWindow(), darkModeFlag, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* 静态域,获取系统版本是否基于MIUI
*/
static {
try {
@SuppressLint("PrivateApi") Class<?> sysClass = Class.forName("android.os.SystemProperties");
Method getStringMethod = sysClass.getDeclaredMethod("get", String.class);
String version = (String) getStringMethod.invoke(sysClass, "ro.miui.ui.version.name");
isMiUi = version.compareTo("V6") >= 0 && Build.VERSION.SDK_INT < 24;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置魅族手机状态栏图标颜色风格
* 可以用来判断是否为Flyme用户
*
* @param window 需要设置的窗口
* @param dark 是否把状态栏字体及图标颜色设置为深色
* @return boolean 成功执行返回true
*/
@SuppressWarnings("JavaReflectionMemberAccess")
public static boolean setMeiZuDarkMode(Window window, boolean dark) {
boolean result = false;
if (Build.VERSION.SDK_INT >= 24) {
return false;
}
if (window != null) {
try {
WindowManager.LayoutParams lp = window.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 (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
@SuppressLint("InlinedApi")
private int getStatusBarLightMode() {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (isMiUi) {
result = 1;
} else if (setMeiZuDarkMode(getWindow(), true)) {
result = 2;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
result = 3;
}
}
return result;
}
@SuppressLint("InlinedApi")
protected void setStatusBarDarkMode() {
int type = getStatusBarLightMode();
if (type == 1) {
setMIUIStatusBarDarkMode();
} else if (type == 2) {
setMeiZuDarkMode(getWindow(), true);
} else if (type == 3) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
}
网友评论