14

如何判断安卓设备是否有物理按键或软件导航栏?我需要根据是否绘制软件导航来更改布局。

例如 HTC Desire C 有硬件键: 在此处输入图像描述

我应该澄清一下 - 我在看导航栏,而不是键盘。回家,回来等。我试过了:

        getResources().getConfiguration().keyboard);
        getResources().getConfiguration().navigation);
        getResources().getConfiguration().navigationHidden);

在两个设备上返回相同的值。

4

4 回答 4

55

通过在第一次启动应用程序并保存到首选项时执行此操作来解决:

public static boolean hasSoftKeys(WindowManager windowManager){
    boolean hasSoftwareKeys = true;
    //c = context; use getContext(); in fragments, and in activities you can 
    //directly access the windowManager();

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){
        Display d = c.getWindowManager().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;

        hasSoftwareKeys =  (realWidth - displayWidth) > 0 ||
                           (realHeight - displayHeight) > 0;
    } else {
        boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        hasSoftwareKeys = !hasMenuKey && !hasBackKey;
    }
    return hasSoftwareKeys;
}
于 2013-02-14T09:50:37.003 回答
2

给你..这应该做你想要的。

@SuppressLint("NewApi")
private static boolean hasSoftNavigation(Context context)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
        return !ViewConfiguration.get(context).hasPermanentMenuKey();
    }
    return false;
}
于 2013-08-02T11:04:21.357 回答
0

private boolean isHardwareKeyboardAvailable() { return getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS; }

此外,这里有更多信息。http://developer.android.com/reference/android/content/res/Configuration.html#keyboard

编辑 - 检查物理键盘

private boolean isPhysicalKeyboardAvailable() { return getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY; }

有关不同配置的更多信息,请查看 http://developer.android.com/reference/android/content/res/Configuration.html#keyboard

我确信其中之一是作品。

第二次编辑 -

查看。已经问过了。

Android:以编程方式检测设备是否有硬件菜单按钮

于 2013-02-13T12:00:42.477 回答
0

基于@serenskye 的解决方案

private fun Activity.hasSoftKeys(): Boolean {
    val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        display
    } else {
        windowManager.defaultDisplay
    } ?: return true

    val realDisplayMetrics = DisplayMetrics()
    display.getRealMetrics(realDisplayMetrics)

    val realHeight = realDisplayMetrics.heightPixels
    val realWidth = realDisplayMetrics.widthPixels

    val displayMetrics = DisplayMetrics()
    display.getMetrics(displayMetrics)

    val displayHeight = displayMetrics.heightPixels
    val displayWidth = displayMetrics.widthPixels

    return realWidth - displayWidth > 0 || realHeight - displayHeight > 0
}
于 2021-05-04T14:46:26.820 回答