StatusBarUtils
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.IntDef;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by Dafen on 2018/5/25.
* 设置状态栏沉浸式工具类
*/
public class StatusBarUtils {
public final static int TYPE_MIUI =0;
public final static int TYPE_FLYME =1;
public final static int TYPE_M =3;//6.0
@IntDef({TYPE_MIUI,
TYPE_FLYME,
TYPE_M})
@Retention(RetentionPolicy.SOURCE)
@interface ViewType {
}
/**
* 修改状态栏颜色,支持4.4以上版本
* @param colorId 颜色
*/
public static void setStatusBarColor(Activity activity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.setStatusBarColor(colorId);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTintManager,需要先将状态栏设置为透明
setTranslucentStatus(activity);
SystemBarTintManager systemBarTintManager =new SystemBarTintManager(activity);
systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
}
}
/**
* 设置状态栏透明
*/
@TargetApi(19)
public static void setTranslucentStatus(Activity activity) {
// 5.0以上系统状态栏透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
//清除透明状态栏
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//设置状态栏颜色必须添加
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);//设置透明
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//19
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 设置沉浸式状态栏
*
* @param fontIconDark 状态栏字体和图标颜色是否为深色
*/
public static void setImmersiveStatusBar(Activity activity,boolean fontIconDark) {
setTranslucentStatus(activity);
if (fontIconDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setStatusBarFontIconDark(activity,TYPE_M);
}else if (OSUtils.isMiui()) {
setStatusBarFontIconDark(activity,TYPE_MIUI);
}else if (OSUtils.isFlyme()) {
setStatusBarFontIconDark(activity,TYPE_FLYME);
}else {//其他情况下我们将状态栏设置为灰色,就不会看不见字体
setStatusBarColor(activity,Color.LTGRAY);//灰色
}
}
}
/**
* 设置文字颜色
*/
public static void setStatusBarFontIconDark(Activity activity,@ViewType int type) {
switch (type) {
case TYPE_MIUI:
setMiuiUI(activity,true);
break;
case TYPE_M:
setCommonUI(activity);
break;
case TYPE_FLYME:
setFlymeUI(activity,true);
break;
}
}
//设置6.0的字体
public static void setCommonUI(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
//设置Flyme的字体
public static void setFlymeUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
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);
}catch (Exception e) {
e.printStackTrace();
}
}
//设置MIUI字体
public static void setMiuiUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
Class clazz = activity.getWindow().getClass();
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
int darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (dark) {//状态栏亮色且黑色字体
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
}else {
extraFlagField.invoke(window, 0, darkModeFlag);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
OSUtils
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by rhm on 2018/1/12.
*/
public class OSUtils {
private static final StringTAG ="Rom";
public static final StringROM_MIUI ="MIUI";
public static final StringROM_EMUI ="EMUI";
public static final StringROM_FLYME ="FLYME";
public static final StringROM_OPPO ="OPPO";
public static final StringROM_SMARTISAN ="SMARTISAN";
public static final StringROM_VIVO ="VIVO";
public static final StringROM_QIKU ="QIKU";
private static final StringKEY_VERSION_MIUI ="ro.miui.ui.version.name";
private static final StringKEY_VERSION_EMUI ="ro.build.version.emui";
private static final StringKEY_VERSION_OPPO ="ro.build.version.opporom";
private static final StringKEY_VERSION_SMARTISAN ="ro.smartisan.version";
private static final StringKEY_VERSION_VIVO ="ro.vivo.os.version";
private static StringsName;
private static StringsVersion;
public static boolean isEmui() {
return check(ROM_EMUI);
}
public static boolean isMiui() {
return check(ROM_MIUI);
}
public static boolean isVivo() {
return check(ROM_VIVO);
}
public static boolean isOppo() {
return check(ROM_OPPO);
}
public static boolean isFlyme() {
return check(ROM_FLYME);
}
public static boolean is360() {
return check(ROM_QIKU) ||check("360");
}
public static boolean isSmartisan() {
return check(ROM_SMARTISAN);
}
public static StringgetName() {
if (sName ==null) {
check("");
}
return sName;
}
public static StringgetVersion() {
if (sVersion ==null) {
check("");
}
return sVersion;
}
public static boolean check(String rom) {
if (sName !=null) {
return sName.equals(rom);
}
if (!TextUtils.isEmpty(sVersion =getProp(KEY_VERSION_MIUI))) {
sName =ROM_MIUI;
}else if (!TextUtils.isEmpty(sVersion =getProp(KEY_VERSION_EMUI))) {
sName =ROM_EMUI;
}else if (!TextUtils.isEmpty(sVersion =getProp(KEY_VERSION_OPPO))) {
sName =ROM_OPPO;
}else if (!TextUtils.isEmpty(sVersion =getProp(KEY_VERSION_VIVO))) {
sName =ROM_VIVO;
}else if (!TextUtils.isEmpty(sVersion =getProp(KEY_VERSION_SMARTISAN))) {
sName =ROM_SMARTISAN;
}else {
sVersion = Build.DISPLAY;
if (sVersion.toUpperCase().contains(ROM_FLYME)) {
sName =ROM_FLYME;
}else {
sVersion = Build.UNKNOWN;
sName = Build.MANUFACTURER.toUpperCase();
}
}
return sName.equals(rom);
}
public static StringgetProp(String name) {
String line =null;
BufferedReader input =null;
try {
Process p = Runtime.getRuntime().exec("getprop " + name);
input =new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
}catch (IOException ex) {
Log.e(TAG, "Unable to read prop " + name, ex);
return null;
}finally {
if (input !=null) {
try {
input.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return line;
}
}
SystemBarTintManager
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class SystemBarTintManager {
public static final int DEFAULT_TINT_COLOR =0x99000000;
private boolean mStatusBarAvailable;
private boolean mStatusBarTintEnabled;
private ViewmStatusBarTintView;
@TargetApi(19)
public SystemBarTintManager(Activity activity) {
Window win = activity.getWindow();
//获取DecorView
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 检查主题中是否有透明的状态栏
int[] attrs = {android.R.attr.windowTranslucentStatus};
TypedArray a = activity.obtainStyledAttributes(attrs);
try {
mStatusBarAvailable = a.getBoolean(0, false);
}finally {
a.recycle();
}
WindowManager.LayoutParams winParams = win.getAttributes();
int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//状态栏透明
if ((winParams.flags & bits) !=0) {
mStatusBarAvailable =true;
}
}
if (mStatusBarAvailable) {
setupStatusBarView(activity, decorViewGroup);
}
}
/**
* 初始化状态栏
*
* @param context
* @param decorViewGroup
*/
private void setupStatusBarView(Activity context, ViewGroup decorViewGroup) {
mStatusBarTintView =new View(context);
//设置高度为Statusbar的高度
FrameLayout.LayoutParams params =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
//默认不显示
mStatusBarTintView.setVisibility(View.GONE);
//decorView添加状态栏高度的View
decorViewGroup.addView(mStatusBarTintView);
}
/**
* 获取状态栏高度
*
* @param activity
* @return
*/
private int getStatusBarHeight(Activity activity) {
int statusBarHeight =0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId >0) {
statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
/**
* 显示状态栏
*/
public void setStatusBarTintEnabled(boolean enabled) {
mStatusBarTintEnabled = enabled;
if (mStatusBarAvailable) {
mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
}
}
/**
* 设置状态栏颜色
*
* @param color
*/
public void setStatusBarTintColor(int color) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundColor(color);
}
}
}