刘海屏

一种是api<28,一般是Android O。一般都是厂商自己修改了Android api,则无法显示调用,只能反射获取判断。
小米:

   // 0:代表不是刘海;1:代表是刘海
    public static int getInt(String key,Activity activity) {
        int result = 0;
        if ("Xiaomi".equals(Build.MANUFACTURER)){//是否是小米手机
            LogUtils.d(TAG,"Xiaomi:true");
            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();
            }
        }else {
            LogUtils.d(TAG,"Xiaomi:false");
        }
        return result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getInt("ro.miui.notch",ParentAreaActivity.this)==1){// 判断是否是刘海
            top=89;// 小米手机的刘海高度,机型不同可能高度不同。
            LogUtils.d(TAG,"notch:true");
        }
    }

一种是api>=28,包括Android P。Android api中提供了方法如下:

public static DisplayCutout isAndroidP(Activity activity){
    View decorView = activity.getWindow().getDecorView();
    if (decorView != null && android.os.Build.VERSION.SDK_INT >= 28){
        WindowInsets windowInsets = decorView.getRootWindowInsets();
        if (windowInsets != null)
            return windowInsets.getDisplayCutout();// <28是没有这个方法的。所以要加版本判断
    }
    return null;
}

你可能感兴趣的:(刘海屏)