根据realsize 与size 判断 另外部分机型如华为P20 pro上亲测也可以
public static boolean isHasNavigationBar(Activity activity) { Point size = new Point(); Point realSize = new Point(); activity.getWindowManager().getDefaultDisplay().getSize(size); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { activity.getWindowManager().getDefaultDisplay().getRealSize(realSize); } else { Display display = activity.getWindowManager().getDefaultDisplay(); Method mGetRawH = null; Method mGetRawW = null; int realWidth = 0; int realHeight = 0; try { mGetRawW = Display.class.getMethod("getRawWidth"); mGetRawH = Display.class.getMethod("getRawHeight"); } catch (NoSuchMethodException e) { e.printStackTrace(); } try { realWidth = (Integer) mGetRawW.invoke(display); realHeight = (Integer) mGetRawH.invoke(display); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } realSize.set(realWidth, realHeight); } if (realSize.equals(size)) { return false; } else { size.y = size.y + getNavigationBarHeight(activity); if (realSize.y < size.y){ return false; } return true; } }
public static int getNavigationBarHeight(Activity activity) { //get navigation bar height. int resourceId = activity.getResources().getIdentifier( "navigation_bar_height", "dimen", "android"); int navigationBarHeight = activity.getResources() .getDimensionPixelSize(resourceId); return navigationBarHeight; }