判断vivo是否有刘海屏
/**
* 判断vivo是否有刘海屏
* https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
* @return
*/
private static boolean hasNotchVIVO() {
try {
Class<?> c = Class.forName("android.util.FtFeature");
Method get = c.getMethod("isFeatureSupport", int.class);
return (boolean) (get.invoke(c, 0x20));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
判断oppo是否有刘海屏
/**
* 判断oppo是否有刘海屏
* https://open.oppomobile.com/wiki/doc#id=10159
* @param activity
* @return
*/
private static boolean hasNotchOPPO(Activity activity) {
return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
小米刘海屏判断
/**
* 判断xiaomi是否有刘海屏
* https://dev.mi.com/console/doc/detail?pId=1293
*
* @param activity
* @return
*/
private static boolean hasNotchXiaoMi(Activity activity) {
return getInt("ro.miui.notch", activity) == 1;
}
/**
* 小米刘海屏判断.
*
* @return 0 if it is not notch ; return 1 means notch
* @throws if the key exceeds 32 characters
*/
public static int getInt(String key, Activity activity) {
int result = 0;
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//参数
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
判断华为是否有刘海屏
/**
* 判断华为是否有刘海屏
* https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
*
* @param activity
* @return
*/
private static boolean hasNotchHw(Activity activity) {
try {
ClassLoader cl = activity.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
return (boolean) get.invoke(HwNotchSizeUtil);
} catch (Exception e) {
return false;
}
}
可以稍微抽取以下工具类
/**
* 是否有刘海屏
*
* @return
*/
public static boolean hasNotchInScreen(Activity activity) {
// android P 以上有标准 API 来判断是否有刘海屏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
View decorView = activity.getWindow().getDecorView();
WindowInsets windowInsets = decorView.getRootWindowInsets();
if (windowInsets != null) {
DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if (displayCutout != null) {
List<Rect> rects = displayCutout.getBoundingRects();
//通过判断是否存在rects来确定是否刘海屏手机
if (rects != null && rects.size() > 0) {
return true;
}
}
}
}
// 通过其他方式判断是否有刘海屏 目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
String manufacturer = Build.MANUFACTURER;
if (StringUtils.isEmpty(manufacturer)) {
return false;
} else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
return hasNotchHw(activity);
} else if (manufacturer.equalsIgnoreCase("xiaomi")) {
return hasNotchXiaoMi(activity);
} else if (manufacturer.equalsIgnoreCase("oppo")) {
return hasNotchOPPO(activity);
} else if (manufacturer.equalsIgnoreCase("vivo")) {
return hasNotchVIVO(activity);
} else {
return false;
}
}
android获取手机型号和手机厂商
1. 获取手机型号:
return android.os.Build.MODEL
2. 获取手机厂商:
return android.os.Build.MANUFACTURER;
这里需要注意的是,SDK1.5是不支持这个参数的。