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

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

问题描述:

如何判断一个Android设备是否有物理按键或软件的导航栏?我需要改变布局依赖于导航软件是否绘制。

How can I tell whether an android device has physical keys or the software navigation bar? I need to change the layout dependant on whether the software navigation is drawn.

例如HTC Desire的C具有硬件按键:

For example the HTC Desire C has hardware keys:

我要澄清 - 林看着导航栏,而不是键盘。回到家,回等我试过:

I should clarify - Im looking at the navigation bar, not the keyboard. Home, back etc. I've tried:

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

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

return the same values on both devices.

做这第一次应用程序启动并保存到preferences解决:

Solved by doing this the first time the app is launched and saving to preferences:

public static boolean hasSoftKeys(WindowManager windowManager){
    boolean hasSoftwareKeys = true;

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