Android 判断是否是刘海屏

news/2024/11/16 8:35:34/

刘海屏手机越来越多,App需要适配。

/*** 判断是否是刘海屏* @return*/
public static boolean hasNotchScreen(Activity activity){if (getInt("ro.miui.notch",activity) == 1 || hasNotchAtHuawei(activity) || hasNotchAtOPPO()|| hasNotchAtVivo(activity) || isAndroidP(activity) != null){ //TODO 各种品牌return true;}return false;
}/*** Android P 刘海屏判断* @param activity* @return*/
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();}return null;
}/*** 小米刘海屏判断.* @return 0 if it is not notch ; return 1 means notch* @throws IllegalArgumentException if the key exceeds 32 characters*/
public static int getInt(String key,Activity activity) {int result = 0;if (isXiaomi()){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;
}/*** 华为刘海屏判断* @return*/
public static boolean hasNotchAtHuawei(Context context) {boolean ret = false;try {ClassLoader classLoader = context.getClassLoader();Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");ret = (boolean) get.invoke(HwNotchSizeUtil);} catch (ClassNotFoundException e) {AppLog.e("hasNotchAtHuawei ClassNotFoundException");} catch (NoSuchMethodException e) {AppLog.e("hasNotchAtHuawei NoSuchMethodException");} catch (Exception e) {AppLog.e( "hasNotchAtHuawei Exception");} finally {return ret;}
}public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
public static final int VIVO_FILLET = 0x00000008;//是否有圆角/*** VIVO刘海屏判断* @return*/
public static boolean hasNotchAtVivo(Context context) {boolean ret = false;try {ClassLoader classLoader = context.getClassLoader();Class FtFeature = classLoader.loadClass("android.util.FtFeature");Method method = FtFeature.getMethod("isFeatureSupport", int.class);ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);} catch (ClassNotFoundException e) {AppLog.e( "hasNotchAtVivo ClassNotFoundException");} catch (NoSuchMethodException e) {AppLog.e(  "hasNotchAtVivo NoSuchMethodException");} catch (Exception e) {AppLog.e(  "hasNotchAtVivo Exception");} finally {return ret;}
}
/*** OPPO刘海屏判断* @return*/
public static boolean hasNotchAtOPPO(Context context) {return  context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}

 

 

// 是否是小米手机
public static boolean isXiaomi() {return "Xiaomi".equals(Build.MANUFACTURER);
}

 

暂时未发现其他刘海屏手机适配方案。。。

 


http://www.ppmy.cn/news/192229.html

相关文章

刘海屏启动空白页适配全面屏

<style name"嘻嘻嘻" parent"AppTheme"><item name"android:windowBackground">drawable/splashpic</item><item name"android:windowFullscreen">true</item><item name"windowNoTitle"&…

Android 检测手机是否是异形屏,刘海屏,水滴屏,挖孔屏

全面屏适配请阅读本人另外一篇博客: https://blog.csdn.net/zhao8856234/article/details/117744924?spm1001.2014.3001.5501 直接上工具类,复制使用即可: public class NotchUtil {private static final String SP_NAME "MY_NOTCH_SP";//保存异形屏Nameprivate…

Android各种屏,刘海屏,打孔屏满屏显示

试了很多种方法&#xff0c;只有这种满屏效果最好。 if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {WindowManager.LayoutParams lp getWindow().getAttributes();lp.layoutInDisplayCutoutMode WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_E…

iOS——判断刘海屏

在写项目时&#xff0c;发现在非刘海屏型号的iPhone上测试的UI界面&#xff0c;有时候在刘海屏iPhone会被遮挡。这时候&#xff0c;我们需要判断当前设备是否是刘海屏&#xff0c;以来对UI代码做相应的适配&#xff0c;以下是学习的几种方案。 判断手机型号 iPhone的发展历史…

Android 浅谈适配全面屏、刘海屏、水滴屏

对刘海屏、水滴屏做适配前&#xff0c;先在此给出一个基本概念&#xff1a;何谓刘海屏&#xff1f;何谓水滴屏&#xff1f; 上述两种屏幕都可以统称为刘海屏&#xff0c;不过对于右侧较小的刘海&#xff0c;业界一般称为水滴屏或美人尖。 目前国内流行的手机厂商主要有&#x…

android兼容小米xiaomi刘海屏解决方案

引用自小米官方文档&#xff0c;这里缩减了一些内容&#xff0c;捡取重要内容。 转载请标明出处&#xff1a; https://blog.csdn.net/DJY1992/article/details/80688376 本文出自:【奥特曼超人的博客】 推荐&#xff1a; android 兼容所有刘海屏的方案大全android 兼容huaw…

Android刘海屏、水滴屏全面屏适配详解,androidui基础教程

适配方式 适配方式有两种&#xff1a; 将targetSdkVersion版本设置到API 24及以上&#xff1b; 这个操作将会为<application> 标签隐式添加一个属性&#xff0c;android:resizeableActivity“true”, 该属性的作用后面将详细说明。 在 标签中增加属性&#xff1a;and…

Android刘海屏、水滴屏全面屏适配详解大厂直通车!

为什么想跳槽&#xff1f; 简单说一下当时的状况&#xff0c;我在这家公司做了两年多&#xff0c;这两年多完成了一个大项目&#xff0c;作为开发的核心主力&#xff0c;开发压力很大&#xff0c;特别是项目上线前的几个月是非常辛苦&#xff0c;几乎每晚都要加班到12点以后&a…