天天看點

Android、iPhone手機劉海屏适配判斷

Android劉海屏适配判斷:

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.view.DisplayCutout;

import java.lang.reflect.Method;

/**
 *
 * @brief: Notch Tools
 */
public class NotchUtils {
    /**
     * 是否有劉海屏
     *
     * @return
     */
    public static boolean hasNotchInScreen(Activity activity) {
        try {
            // android P以上有标準 API 來判斷是否有劉海屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
                if (displayCutout != null) {
                    // 說明有劉海屏
                    return true;
                }
            } else {
                // 通過其他方式判斷是否有劉海屏  目前官方提供有開發文檔的就 小米,vivo,華為(榮耀),oppo , meizu
                String manufacturer = Build.MANUFACTURER;
                if (TextUtils.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 if (manufacturer.equalsIgnoreCase("meizu")) {
                    return hasNotchMeizu(activity);
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            LogUtils.print(e.getMessage());
            return false;
        }
        return false;
    }

    /**
     * 判斷vivo是否有劉海屏
     * https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchVIVO(Activity activity) {
        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是否有劉海屏
     * 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) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("getInt", String.class, int.class);
            return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判斷華為是否有劉海屏
     * 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;
        }
    }

    /**
     * 判斷魅族是否有劉海屏
     *
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchMeizu(Activity activity) {
        try {
            Class clazz = Class.forName("flyme.config.FlymeFeature");
            Field field = clazz.getDeclaredField("IS_FRINGE_DEVICE");
            return (boolean) field.get(null);
        } catch (Exception e) {
            return false;
        }
    }
}

           

iPhone劉海屏适配判斷:

(BOOL)hasNotch {
    BOOL bHasNotch = NO;
    //判斷是否是手機
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return bHasNotch;
    }

    //手機判斷
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            bHasNotch = YES;
        }
    }

    return bHasNotch;
}