转自:https://blog.csdn.net/u013541140/article/details/100065336
今天看到这位大神的代码,感觉很受用,就自己动手按自己的习惯敲了一下,这里我只做了5.0、6.0以上的适配。以下的没有做了,感觉没有什么必要。
StatusBarUtils.java
package com.cg.statusbarlearn.untils;import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.graphics.ColorUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class StatusBarUtils {/*** 得到状态栏的高度* @param mContext 上下文* @return 状态栏高度*/public static int getStatusBarHeight(Context mContext){int statusBarHeight = 0;try{int resourceId = mContext.getResources().getIdentifier("status_bar_height","dimen","android");if(resourceId > 0){statusBarHeight = mContext.getResources().getDimensionPixelOffset(resourceId);}}catch (Exception ex){ex.printStackTrace();}return statusBarHeight;}/*** 设置状态栏的颜色* @param window 当前窗口,来自activiy或dialog等持有的窗口* @param color 颜色值*/public static void setStatusBarColor(@NonNull Window window, @ColorInt int color) {//Android 5.0+if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);window.setStatusBarColor(color);setStatusBarTextDark(window, !isDarkColor(color));}}/*** 对于activity进行修改* @param context activity的context* @param color 颜色*/public static void setStatusBarColor(Context context, @ColorInt int color) {if (context instanceof Activity) {setStatusBarColor(((Activity) context).getWindow(), color);}}/*** 设置状态栏文字的颜色 Android 6.0以上* @param window 窗口* @param isDark 是否是深色背景*/private static void setStatusBarTextDark(Window window, boolean isDark) {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {View decorView = window.getDecorView();int systemUiVisibility = decorView.getSystemUiVisibility();if (isDark) {decorView.setSystemUiVisibility(systemUiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);} else {decorView.setSystemUiVisibility(systemUiVisibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);}}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {switch (OSUtils.getRomType()) {case MIUI:setMIUIDark(window, isDark);break;case Flyme:setFlymeDark(window, isDark);break;default:}}}public static void setStatusBarTextDark(Context context, boolean isDark) {if (context instanceof Activity) {setStatusBarTextDark(((Activity) context).getWindow(), isDark);}}/*** 根据状态栏背景颜色的深浅而自动设置文字的颜色* @param color 颜色* @return 返回值*/public static boolean isDarkColor(@ColorInt int color) {return ColorUtils.calculateLuminance(color) < 0.5;}/*** 小米手机在android5.0-6.0之间支持的修改状态栏文字颜色* @param window window* @param isDark 是否是暗色*/private static void setMIUIDark(Window window, boolean isDark) {try {Class<? extends Window> clazz = window.getClass();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(window, isDark ? darkModeFlag : 0, darkModeFlag);} catch (Exception e) {e.printStackTrace();}}/*** 魅族手机在android5.0-6.0之间支持的修改状态栏文字颜色* @param window window* @param isDark 是否是暗色*/private static void setFlymeDark(Window window, boolean isDark) {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 (isDark) {value |= bit;} else {value &= ~bit;}meizuFlags.setInt(lp, value);window.setAttributes(lp);} catch (Exception e) {e.printStackTrace();}}}/*** 当背景是图片时,设置沉浸试* @param window 窗口*/public static void setStatusBarTransparent(@NonNull Window window) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);window.setStatusBarColor(Color.TRANSPARENT);}}public static void setStatusBarTransparent(Context context) {if (context instanceof Activity) {setStatusBarTransparent(((Activity) context).getWindow());}}
}
OSUtils.java
package com.cg.statusbarlearn.untils;import android.os.Environment;
import android.text.TextUtils;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** <pre>* author : Senh Linsh* github : https://github.com/SenhLinsh* date : 2017/11/10* desc : 工具类: OS 系统相关* 简介 : 由于国内定制系统的泛滥, 不同定制系统的一些功能或实现方法会有所不同, 如果需要做到足够好的适配工作, 需要* 对不同的定制系统做一些专门的适配.* API : 获取 ROM 类型等** Deprecated :* 在工具类中对 ROM 类型进行判断,显得有些力不从心,所以单独创建了新的项目,用于判断 ROM,获取其版本号。** 敬请期待:* https://github.com/SenhLinsh/Android-ROM-Identifier* </pre>*/public class OSUtils {private static final String KEY_DISPLAY_ID = "ro.build.display.id";private static final String KEY_BASE_OS_VERSION = "ro.build.version.base_os";private static final String KEY_CLIENT_ID_BASE = "ro.com.google.clientidbase";// 小米 : MIUIprivate static final String KEY_MIUI_VERSION = "ro.build.version.incremental"; // "7.6.15"private static final String KEY_MIUI_VERSION_NANE = "ro.miui.ui.version.name"; // "V8"private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; // "6"private static final String VALUE_MIUI_CLIENT_ID_BASE = "android-xiaomi";// 华为 : EMUIprivate static final String KEY_EMUI_VERSION = "ro.build.version.emui"; // "EmotionUI_3.0"private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level"; //private static final String KEY_EMUI_SYSTEM_VERSION = "ro.confg.hw_systemversion"; //// "T1-A21wV100R001C233B008_SYSIMG"// 魅族 : Flymeprivate static final String KEY_FLYME_PUBLISHED = "ro.flyme.published"; // "true"private static final String KEY_FLYME_SETUP = "ro.meizu.setupwizard.flyme"; // "true"private static final String VALUE_FLYME_DISPLAY_ID_CONTAIN = "Flyme"; // "Flyme OS 4.5.4.2U"// OPPO : ColorOSprivate static final String KEY_COLOROS_VERSION = "ro.oppo.theme.version"; // "703"private static final String KEY_COLOROS_THEME_VERSION = "ro.oppo.version"; // ""private static final String KEY_COLOROS_ROM_VERSION = "ro.rom.different.version"; //private static final String VALUE_COLOROS_BASE_OS_VERSION_CONTAIN = "OPPO"; // "OPPO/R7sm// "ColorOS2.1"// /R7sm:5.1.1/LMY47V/1440928800:user/release-keys"private static final String VALUE_COLOROS_CLIENT_ID_BASE = "android-oppo";// vivo : FuntouchOSprivate static final String KEY_FUNTOUCHOS_BOARD_VERSION = "ro.vivo.board.version"; // "MD"private static final String KEY_FUNTOUCHOS_OS_NAME = "ro.vivo.os.name"; // "Funtouch"private static final String KEY_FUNTOUCHOS_OS_VERSION = "ro.vivo.os.version"; // "3.0"private static final String KEY_FUNTOUCHOS_DISPLAY_ID = "ro.vivo.os.build.display.id"; //// "FuntouchOS_3.0"private static final String KEY_FUNTOUCHOS_ROM_VERSION = "ro.vivo.rom.version"; // "rom_3.1"private static final String VALUE_FUNTOUCHOS_CLIENT_ID_BASE = "android-vivo";// Samsungprivate static final String VALUE_SAMSUNG_BASE_OS_VERSION_CONTAIN = "samsung"; // "samsung// /zeroltezc/zeroltechn:6.0.1/MMB29K/G9250ZCU2DQD1:user/release-keys"private static final String VALUE_SAMSUNG_CLIENT_ID_BASE = "android-samsung";// Sonyprivate static final String KEY_SONY_PROTOCOL_TYPE = "ro.sony.irremote.protocol_type"; // "2"private static final String KEY_SONY_ENCRYPTED_DATA = "ro.sony.fota.encrypteddata"; //private static final String VALUE_SONY_CLIENT_ID_BASE = "android-sonyericsson";// "supported"// 乐视 : euiprivate static final String KEY_EUI_VERSION = "ro.letv.release.version"; // "5.9.023S"private static final String KEY_EUI_VERSION_DATE = "ro.letv.release.version_date"; // "5.9// .023S_03111"private static final String KEY_EUI_NAME = "ro.product.letv_name"; // "乐1s"private static final String KEY_EUI_MODEL = "ro.product.letv_model"; // "Letv X500"// 金立 : amigoprivate static final String KEY_AMIGO_ROM_VERSION = "ro.gn.gnromvernumber"; // "GIONEE ROM5.0// .16"private static final String KEY_AMIGO_SYSTEM_UI_SUPPORT = "ro.gn.amigo.systemui.support"; //private static final String VALUE_AMIGO_DISPLAY_ID_CONTAIN = "amigo"; // "amigo3.5.1"// "yes"private static final String VALUE_AMIGO_CLIENT_ID_BASE = "android-gionee";// 酷派 : yulongprivate static final String KEY_YULONG_VERSION_RELEASE = "ro.yulong.version.release"; // "5.1// .046.P1.150921.8676_M01"private static final String KEY_YULONG_VERSION_TAG = "ro.yulong.version.tag"; // "LC"private static final String VALUE_YULONG_CLIENT_ID_BASE = "android-coolpad";// HTC : Senseprivate static final String KEY_SENSE_BUILD_STAGE = "htc.build.stage"; // "2"private static final String KEY_SENSE_BLUETOOTH_SAP = "ro.htc.bluetooth.sap"; // "true"private static final String VALUE_SENSE_CLIENT_ID_BASE = "android-htc-rev";// LG : LGprivate static final String KEY_LG_SW_VERSION = "ro.lge.swversion"; // "D85720b"private static final String KEY_LG_SW_VERSION_SHORT = "ro.lge.swversion_short"; // "V20b"private static final String KEY_LG_FACTORY_VERSION = "ro.lge.factoryversion"; // "LGD857AT-00// -V20b-CUO-CN-FEB-17-2015+0"// 联想private static final String KEY_LENOVO_DEVICE = "ro.lenovo.device"; // "phone"private static final String KEY_LENOVO_PLATFORM = "ro.lenovo.platform"; // "qualcomm"private static final String KEY_LENOVO_ADB = "ro.lenovo.adb"; // "apkctl,speedup"private static final String VALUE_LENOVO_CLIENT_ID_BASE = "android-lenovo";/*** ROM 类型*/private static final ROM ROM_TYPE = initRomType();/*** 获取 ROM 类型** @return ROM*/public static ROM getRomType() {return ROM_TYPE;}/*** 初始化 ROM 类型*/private static ROM initRomType() {ROM rom = ROM.Other;FileInputStream is = null;try {Properties buildProperties = new Properties();is = new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"));buildProperties.load(is);if (buildProperties.containsKey(KEY_MIUI_VERSION_NANE) || buildProperties.containsKey(KEY_MIUI_VERSION_CODE)) {// MIUIrom = ROM.MIUI;if (buildProperties.containsKey(KEY_MIUI_VERSION_NANE)) {String versionName = buildProperties.getProperty(KEY_MIUI_VERSION_NANE);if (!TextUtils.isEmpty(versionName) && versionName.matches("[Vv]\\d+")) { // V8try {rom.setBaseVersion(Integer.parseInt(versionName.split("[Vv]")[1]));} catch (Exception e) {e.printStackTrace();}}}if (buildProperties.containsKey(KEY_MIUI_VERSION)) {String versionStr = buildProperties.getProperty(KEY_MIUI_VERSION);if (!TextUtils.isEmpty(versionStr) && versionStr.matches("[\\d.]+")) {rom.setVersion(versionStr);}}} else if (buildProperties.containsKey(KEY_EMUI_VERSION) || buildProperties.containsKey(KEY_EMUI_API_LEVEL)|| buildProperties.containsKey(KEY_EMUI_SYSTEM_VERSION)) {// EMUIrom = ROM.EMUI;if (buildProperties.containsKey(KEY_EMUI_VERSION)) {String versionStr = buildProperties.getProperty(KEY_EMUI_VERSION);Matcher matcher = Pattern.compile("EmotionUI_([\\d.]+)").matcher(versionStr); // EmotionUI_3.0if (!TextUtils.isEmpty(versionStr) && matcher.find()) {try {String version = matcher.group(1);rom.setVersion(version);rom.setBaseVersion(Integer.parseInt(version.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_FLYME_SETUP) || buildProperties.containsKey(KEY_FLYME_PUBLISHED)) {// Flymerom = ROM.Flyme;if (buildProperties.containsKey(KEY_DISPLAY_ID)) {String versionStr = buildProperties.getProperty(KEY_DISPLAY_ID);Matcher matcher =Pattern.compile("Flyme[^\\d]*([\\d.]+)[^\\d]*").matcher(versionStr);// Flyme OS 4.5.4.2Uif (!TextUtils.isEmpty(versionStr) && matcher.find()) {try {String version = matcher.group(1);rom.setVersion(version);rom.setBaseVersion(Integer.parseInt(version.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_COLOROS_VERSION) || buildProperties.containsKey(KEY_COLOROS_THEME_VERSION)|| buildProperties.containsKey(KEY_COLOROS_ROM_VERSION)) {// ColorOSrom = ROM.ColorOS;if (buildProperties.containsKey(KEY_COLOROS_ROM_VERSION)) {String versionStr = buildProperties.getProperty(KEY_COLOROS_ROM_VERSION);Matcher matcher = Pattern.compile("ColorOS([\\d.]+)").matcher(versionStr); //// ColorOS2.1if (!TextUtils.isEmpty(versionStr) && matcher.find()) {try {String version = matcher.group(1);rom.setVersion(version);rom.setBaseVersion(Integer.parseInt(version.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_FUNTOUCHOS_OS_NAME) || buildProperties.containsKey(KEY_FUNTOUCHOS_OS_VERSION)|| buildProperties.containsKey(KEY_FUNTOUCHOS_DISPLAY_ID)) {// FuntouchOSrom = ROM.FuntouchOS;if (buildProperties.containsKey(KEY_FUNTOUCHOS_OS_VERSION)) {String versionStr = buildProperties.getProperty(KEY_FUNTOUCHOS_OS_VERSION);if (!TextUtils.isEmpty(versionStr) && versionStr.matches("[\\d.]+")) { // 3.0try {rom.setVersion(versionStr);rom.setBaseVersion(Integer.parseInt(versionStr.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_EUI_VERSION) || buildProperties.containsKey(KEY_EUI_NAME)|| buildProperties.containsKey(KEY_EUI_MODEL)) {// EUIrom = ROM.EUI;if (buildProperties.containsKey(KEY_EUI_VERSION)) {String versionStr = buildProperties.getProperty(KEY_EUI_VERSION);Matcher matcher = Pattern.compile("([\\d.]+)[^\\d]*").matcher(versionStr); //// 5.9.023Sif (!TextUtils.isEmpty(versionStr) && matcher.find()) {try {String version = matcher.group(1);rom.setVersion(version);rom.setBaseVersion(Integer.parseInt(version.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_AMIGO_ROM_VERSION) || buildProperties.containsKey(KEY_AMIGO_SYSTEM_UI_SUPPORT)) {// amigorom = ROM.AmigoOS;if (buildProperties.containsKey(KEY_DISPLAY_ID)) {String versionStr = buildProperties.getProperty(KEY_DISPLAY_ID);Matcher matcher =Pattern.compile("amigo([\\d.]+)[a-zA-Z]*").matcher(versionStr); //// "amigo3.5.1"if (!TextUtils.isEmpty(versionStr) && matcher.find()) {try {String version = matcher.group(1);rom.setVersion(version);rom.setBaseVersion(Integer.parseInt(version.split("\\.")[0]));} catch (Exception e) {e.printStackTrace();}}}} else if (buildProperties.containsKey(KEY_SONY_PROTOCOL_TYPE) || buildProperties.containsKey(KEY_SONY_ENCRYPTED_DATA)) {// Sonyrom = ROM.Sony;} else if (buildProperties.containsKey(KEY_YULONG_VERSION_RELEASE) || buildProperties.containsKey(KEY_YULONG_VERSION_TAG)) {// YuLongrom = ROM.YuLong;} else if (buildProperties.containsKey(KEY_SENSE_BUILD_STAGE) || buildProperties.containsKey(KEY_SENSE_BLUETOOTH_SAP)) {// Senserom = ROM.Sense;} else if (buildProperties.containsKey(KEY_LG_SW_VERSION) || buildProperties.containsKey(KEY_LG_SW_VERSION_SHORT)|| buildProperties.containsKey(KEY_LG_FACTORY_VERSION)) {// LGrom = ROM.LG;} else if (buildProperties.containsKey(KEY_LENOVO_DEVICE) || buildProperties.containsKey(KEY_LENOVO_PLATFORM)|| buildProperties.containsKey(KEY_LENOVO_ADB)) {// Lenovorom = ROM.Lenovo;} else if (buildProperties.containsKey(KEY_DISPLAY_ID)) {String displayId = buildProperties.getProperty(KEY_DISPLAY_ID);if (!TextUtils.isEmpty(displayId)) {if (displayId.contains(VALUE_FLYME_DISPLAY_ID_CONTAIN)) {return ROM.Flyme;} else if (displayId.contains(VALUE_AMIGO_DISPLAY_ID_CONTAIN)) {return ROM.AmigoOS;}}} else if (buildProperties.containsKey(KEY_BASE_OS_VERSION)) {String baseOsVersion = buildProperties.getProperty(KEY_BASE_OS_VERSION);if (!TextUtils.isEmpty(baseOsVersion)) {if (baseOsVersion.contains(VALUE_COLOROS_BASE_OS_VERSION_CONTAIN)) {return ROM.ColorOS;} else if (baseOsVersion.contains(VALUE_SAMSUNG_BASE_OS_VERSION_CONTAIN)) {return ROM.SamSung;}}} else if (buildProperties.containsKey(KEY_CLIENT_ID_BASE)) {String clientIdBase = buildProperties.getProperty(KEY_CLIENT_ID_BASE);switch (clientIdBase) {case VALUE_MIUI_CLIENT_ID_BASE:return ROM.MIUI;case VALUE_COLOROS_CLIENT_ID_BASE:return ROM.ColorOS;case VALUE_FUNTOUCHOS_CLIENT_ID_BASE:return ROM.FuntouchOS;case VALUE_SAMSUNG_CLIENT_ID_BASE:return ROM.SamSung;case VALUE_SONY_CLIENT_ID_BASE:return ROM.Sony;case VALUE_YULONG_CLIENT_ID_BASE:return ROM.YuLong;case VALUE_SENSE_CLIENT_ID_BASE:return ROM.Sense;case VALUE_LENOVO_CLIENT_ID_BASE:return ROM.Lenovo;case VALUE_AMIGO_CLIENT_ID_BASE:return ROM.AmigoOS;default:break;}}} catch (IOException e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}return rom;}public enum ROM {MIUI, // 小米Flyme, // 魅族EMUI, // 华为ColorOS, // OPPOFuntouchOS, // vivoSmartisanOS, // 锤子EUI, // 乐视Sense, // HTCAmigoOS, // 金立_360OS, // 奇酷360NubiaUI, // 努比亚H2OS, // 一加YunOS, // 阿里巴巴YuLong, // 酷派SamSung, // 三星Sony, // 索尼Lenovo, // 联想LG, // LGGoogle, // 原生Other; // CyanogenMod, Lewa OS, 百度云OS, Tencent OS, 深度OS, IUNI OS, Tapas OS, Mokeeprivate int baseVersion = -1;private String version;public int getBaseVersion() {return baseVersion;}void setBaseVersion(int baseVersion) {this.baseVersion = baseVersion;}public String getVersion() {return version;}void setVersion(String version) {this.version = version;}}
}