studio library jetpack component android android-layout android-ui android-navigation

library - ¿Detectar la disponibilidad de la barra de navegación suave en un dispositivo Android de forma progresiva?



navigation editor android studio (5)

Como sé se puede detectar por

boolean hasSoftKey = ViewConfiguration.get(context).hasPermanentMenuKey();

Pero se requieren APIs 14+

Si la solución anterior no funciona para usted, pruebe el método siguiente

public boolean isNavigationBarAvailable(){ boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK); boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME); return (!(hasBackKey && hasHomeKey)); }

Estoy tratando de determinar la barra de navegación suave a través del programa de Android. No encontré una manera directa de determinar. ¿Hay alguna forma de encontrar la disponibilidad de la barra de navegación?

La imagen de la barra de navegación suave está aquí.


El siguiente método funcionó para mí y se probó en muchos dispositivos.

public boolean hasNavBar (Resources resources) { int id = resources.getIdentifier("config_showNavigationBar", "bool", "android"); return id > 0 && resources.getBoolean(id); }

Nota: Verificado este método en dispositivo real.


Es un truco pero funciona bien. Intentalo.

public static boolean hasSoftKeys(WindowManager windowManager){ Display d = windowManager.getDefaultDisplay(); DisplayMetrics realDisplayMetrics = new DisplayMetrics(); d.getRealMetrics(realDisplayMetrics); int realHeight = realDisplayMetrics.heightPixels; int realWidth = realDisplayMetrics.widthPixels; DisplayMetrics displayMetrics = new DisplayMetrics(); d.getMetrics(displayMetrics); int displayHeight = displayMetrics.heightPixels; int displayWidth = displayMetrics.widthPixels; return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; }


Prueba este método, de esta manera puedes detectar si existe la barra de navegación.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public boolean hasNavBar(Context context) { Point realSize = new Point(); Point screenSize = new Point(); boolean hasNavBar = false; DisplayMetrics metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); realSize.x = metrics.widthPixels; realSize.y = metrics.heightPixels; getWindowManager().getDefaultDisplay().getSize(screenSize); if (realSize.y != screenSize.y) { int difference = realSize.y - screenSize.y; int navBarHeight = 0; Resources resources = context.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { navBarHeight = resources.getDimensionPixelSize(resourceId); } if (navBarHeight != 0) { if (difference == navBarHeight) { hasNavBar = true; } } } return hasNavBar; }


La respuesta aceptada debería funcionar bien en la mayoría de los dispositivos reales, pero no funciona en los emuladores.

Sin embargo, en Android 4.0 y superior, hay una API interna que también funciona en los emuladores: IWindowManager.hasNavigationBar() . Se puede acceder a ella mediante la reflexión:

/** * Returns {@code null} if this couldn''t be determined. */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @SuppressLint("PrivateApi") public static Boolean hasNavigationBar() { try { Class<?> serviceManager = Class.forName("android.os.ServiceManager"); IBinder serviceBinder = (IBinder)serviceManager.getMethod("getService", String.class).invoke(serviceManager, "window"); Class<?> stub = Class.forName("android.view.IWindowManager$Stub"); Object windowManagerService = stub.getMethod("asInterface", IBinder.class).invoke(stub, serviceBinder); Method hasNavigationBar = windowManagerService.getClass().getMethod("hasNavigationBar"); return (boolean)hasNavigationBar.invoke(windowManagerService); } catch (ClassNotFoundException | ClassCastException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Log.w("YOUR_TAG_HERE", "Couldn''t determine whether the device has a navigation bar", e); return null; } }