一:首先判断机型是属于那种:
//小米和华为做了两种兼容判断:需要权限的情况下,无法读取配置,只能用反射
public final class OSHelper {
public static final String SYS_EMUI = "sys_emui";
public static final String SYS_MIUI = "sys_miui";
public static final String SYS_FLYME = "sys_flyme";
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";
public static boolean isFlyme() {
try {
final Method method = Build.class.getMethod("hasSmartBar");
return method != null;
} catch (final Exception e) {
return false;
}
}
public static boolean isEMUI() {
return TextUtils.equals(getSystem(), SYS_EMUI);
}
public static boolean isMIUI() {
return TextUtils.equals(getSystem(), SYS_MIUI);
}
private static String getSystem() {
String SYS = "";
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
if (!TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_CODE, ""))
|| !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_NAME, ""))
|| !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_INTERNAL_STORAGE, ""))) {
SYS = SYS_MIUI;//小米
} else if (!TextUtils.isEmpty(getSystemProperty(KEY_EMUI_API_LEVEL, ""))
|| !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_VERSION, ""))
|| !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, ""))) {
SYS = SYS_EMUI;//华为
}
return SYS;
} else {
try {
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {
SYS = SYS_MIUI;//小米
} else if (prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
|| prop.getProperty(KEY_EMUI_VERSION, null) != null
|| prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null) {
SYS = SYS_EMUI;//华为
}
} catch (IOException e) {
e.printStackTrace();
return SYS;
} finally {
return SYS;
}
}
}
private static String getSystemProperty(String key, String defaultValue) {
try {
Class<?> clz = Class.forName("android.os.SystemProperties");
Method get = clz.getMethod("get", String.class, String.class);
return (String) get.invoke(clz, key, defaultValue);
} catch (Exception e) {
}
return defaultValue;
}
}
二:根据不同类型获取是否有Navigator
MIUI版本是根据小米提供的方法获取判断,已测可以
其它版本的根据app屏幕和实际屏幕进行对比判断是否存在,华为的,模拟器已测是可以
//判断是否存在NavigationBar
public static boolean checkDeviceHasNavigationBar(Context context) {
if (context != null) {
if (context instanceof Activity) {
if (OSHelper.isMIUI()) {
boolean force_fsg_nav_bar = Settings.Global.getInt(context.getContentResolver(), "force_fsg_nav_bar", 0) != 0;
//true 是手势,默认是 false
return !force_fsg_nav_bar;//是否 有虚拟键盘
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y;
} else {
boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !menu && !back;
}
} else {
return false;
}
} else {
return false;
}
}
网友评论