Android手機劉海屏(附工具類)

工具類

根據VIVO、OPPO、華爲官方文檔,這裏整理了一個劉海屏工具類,判斷設備是否爲劉海屏,其餘廠商公佈相關方法後也會在此更新。android

OPPO:工具

/**
     * OPPO
     *
     * @param context Context
     * @return hasNotch
     */
    public static boolean hasNotchInOppo(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

VIVO:測試

/**
     * VIVO
     * <p>
     * android.util.FtFeature
     * public static boolean isFeatureSupport(int mask);
     * <p>
     * 參數:
     * 0x00000020表示是否有凹槽;
     * 0x00000008表示是否有圓角。
     *
     * @param context Context
     * @return hasNotch
     */
    public static boolean hasNotchInVivo(Context context) {
        boolean hasNotch = false;
        try {
            ClassLoader cl = context.getClassLoader();
            Class FtFeature = cl.loadClass("android.util.FtFeature");
            Method get = FtFeature.getMethod("isFeatureSupport");
            hasNotch = (boolean) get.invoke(FtFeature, new Object[]{0x00000020});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasNotch;
    }

華爲:ui

/**
     * HUAWEI
     * com.huawei.android.util.HwNotchSizeUtil
     * public static boolean hasNotchInScreen()
     *
     * @param context Context
     * @return hasNotch
     */
    public static boolean hasNotchInHuawei(Context context) {
        boolean hasNotch = false;
        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            hasNotch = (boolean) get.invoke(HwNotchSizeUtil);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasNotch;
    }

除了OPPO的判斷簡單點外,其餘兩家廠商都是用反射來獲取劉海屏幕信息的。除了VIVO外,另外兩家設備都測試過了,有相關設備的開發者能夠自行測試一下,歡迎評論私信反饋。spa

結語

最後,不得不感嘆蘋果的號召力。跟風也好,抄襲也罷,最爲開發者,吐槽以後,仍是得作好應用適配。.net

 

 

---小米的方案:code

1.如何判斷設備爲 Notch 機型blog

系統增長了 property ro.miui.notch,值爲1時則是 Notch 屏手機。開發




SystemProperties.getInt("ro.miui.notch", 0) == 1;

 https://blog.csdn.net/djy1992/article/details/80688376文檔

 

 https://blog.csdn.net/DJY1992/article/details/80689632

 

轉載:  https://juejin.im/entry/5acf72555188255c9323ad6d

相關文章
相關標籤/搜索