如何判断一个Android设备是否有硬键

时间:2013-02-13 11:58:00

标签: android

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

例如,HTC Desire C有硬件密钥: enter image description here

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

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

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

4 个答案:

答案 0 :(得分:48)

第一次启动应用并保存到偏好设置时解决此问题:

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;
}

答案 1 :(得分: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;
}

答案 2 :(得分: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: Programmatically detect if device has hardware menu button

答案 3 :(得分: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
}