How to detect soft menu key available in android device?

吃可爱长大的小学妹 提交于 2019-12-25 17:43:04

问题


I would like to check menu key presence in the device in android application. To achieve that i used following code to detect weather device is having hardware menu or not and it is working fine

!ViewConfiguration.get(ScsCommander.getInstance().getApplicationContext()).hasPermanentMenuKey()

But i did not find a logic to find weather the device is having soft menu present or not.

Please suggest me is there any way to detect soft menu is available or not in the device.


回答1:


There is no reliable/clean way to check if soft menu (aka Navigation bar) is present or not!

You may try using below code (not tested on all devices and not a reliable solution anyways):

boolean hasNavBar(Context context) {
    Resources resources = context.getResources();
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        return resources.getBoolean(id);
    } else {    // Check for keys
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }
}

Here, we are fetching the resource identifier using Resources class. We are looking for a resource "navigation bar" which is passed as the 1st parameter.

The getIdentifier returns the associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

In case if this approach fails, in else we are trying to see check if certain physical keys are present on the device like back or Home which usually constitute Navigation bar.



来源:https://stackoverflow.com/questions/31405635/how-to-detect-soft-menu-key-available-in-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!