Android判断是否为刘海屏

主要总结主流品牌小米、华为、oppo、vivo的刘海屏判断。在某些特殊页面需要适配刘海屏时,可以用以下方法判断。
或者判断屏幕比例是否大于2。

/**
* 小米刘海屏判断.
*/
 1     public static int getInt(String key,Activity activity) {
 2         int result = 0;
 3         if (isXiaomi()){
 4             try {
 5                 ClassLoader classLoader = activity.getClassLoader();
 6                 @SuppressWarnings("rawtypes")
 7                 Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
 8                 //参数类型
 9                 @SuppressWarnings("rawtypes")
10                 Class[] paramTypes = new Class[2];
11                 paramTypes[0] = String.class;
12                 paramTypes[1] = int.class;
13                 Method getInt = SystemProperties.getMethod("getInt", paramTypes);
14                 //参数
15                 Object[] params = new Object[2];
16                 params[0] = new String(key);
17                 params[1] = new Integer(0);
18                 result = (Integer) getInt.invoke(SystemProperties, params);
19 
20             } catch (ClassNotFoundException e) {
21                 e.printStackTrace();
22             } catch (NoSuchMethodException e) {
23                 e.printStackTrace();
24             } catch (IllegalAccessException e) {
25                 e.printStackTrace();
26             } catch (IllegalArgumentException e) {
27                 e.printStackTrace();
28             } catch (InvocationTargetException e) {
29                 e.printStackTrace();
30             }
31         }
32         return result;
33     }
1     // 是否是小米手机
2     public static boolean isXiaomi() {
3         return "Xiaomi".equals(Build.MANUFACTURER);
4     }
/**
* 华为刘海屏判断
*/
 1     public static boolean hasNotchAtHuawei(Context context) {
 2         boolean ret = false;
 3         try {
 4             ClassLoader cl = context.getClassLoader();
 5             Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
 6             Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
 7             ret = (boolean) get.invoke(HwNotchSizeUtil);
 8         } catch (ClassNotFoundException e) {
 9             LogUtil.e("Huawei", "hasNotchInScreen ClassNotFoundException");
10         } catch (NoSuchMethodException e) {
11             LogUtil.e("Huawei", "hasNotchInScreen NoSuchMethodException");
12         } catch (Exception e) {
13             LogUtil.e("Huawei", "hasNotchInScreen Exception");
14         } finally {
15             return ret;
16         }
17     }
/**
* VIVO刘海屏判断
*/
 1     public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
 2     public static final int VIVO_FILLET = 0x00000008;//是否有圆角
 3 
 4     public static boolean hasNotchAtVivo(Context context) {
 5         boolean ret = false;
 6         try {
 7             ClassLoader classLoader = context.getClassLoader();
 8             Class FtFeature = classLoader.loadClass("android.util.FtFeature");
 9             Method method = FtFeature.getMethod("isFeatureSupport", int.class);
10             ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
11         } catch (ClassNotFoundException e) {
12             LogUtil.e( "Vivo","hasNotchAtVivo ClassNotFoundException");
13         } catch (NoSuchMethodException e) {
14             LogUtil.e(  "Vivo","hasNotchAtVivo NoSuchMethodException");
15         } catch (Exception e) {
16             LogUtil.e(  "Vivo","hasNotchAtVivo Exception");
17         } finally {
18             return ret;
19         }
20     }
/**
* OPPO刘海屏判断
*/
1     public static boolean hasNotchAtOPPO(Context context) {
2         return  context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
3     }

By LiYing




猜你喜欢

转载自www.cnblogs.com/widgetbox/p/9767790.html