The accepted answer devrait fonctionner correctement sur la plupart des appareils réels, mais il ne fonctionne pas dans les émulateurs.
Toutefois, dans Android 4.0 et versions ultérieures, il existe une API interne qui fonctionne également sur les émulateurs: IWindowManager.hasNavigationBar(). Vous pouvez y accéder en utilisant la réflexion:
/**
* 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;
}
}
Ce code fonctionne mais au lieu de 'android' utiliser '"android"' – BlueMango
Renvoie 'false' dans les deux cas. – CoolMind
Notez que ceci renvoie toujours 'false' sur les émulateurs Android. Voir [ce message de validation] (https://github.com/android/platform_frameworks_base/commit/0c4ccff36930ff4f0292b94ad51e164c9fa060a3) pour une explication. – Sam