本文介绍了如何检测在Android设备上可用软菜单键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查菜单键presence在android应用设备。为了实现这一目标,我用下面的code检测天气器件是具有硬件菜单或没有,这是工作的罚款

<$p$p><$c$c>!ViewConfiguration.get(ScsCommander.getInstance().getApplicationContext()).hasPermanentMenuKey()

但我没有找到一个逻辑来查找天气预报设备有软菜单 present与否。

请建议我有什么方法来检测软菜单是否可用在设备中。


解决方案

有没有可靠的/干净的方法来检查,如果软菜单(又名导航栏)是present还是不行!

您可以尝试使用下面code(上所有设备没有经过测试,而不是一个反正可靠的解决方案):

 布尔hasNavBar(上下文的背景下){
    资源资源= context.getResources();
    INT ID = resources.getIdentifier(config_showNavigationBar,布尔,机器人);
    如果(ID大于0){
        返回resources.getBoolean(ID);
    }其他{//检查钥匙
        布尔hasMenuKey = ViewConfiguration.get(上下文).hasPermanentMenuKey();
        布尔hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEY code_BACK);
        返回hasMenuKey和放大器;!&安培; !hasBackKey;
    }
}

在这里,我们使用资源类获取资源标识符。我们正在寻找这是作为第一个参数传递的资源导航栏。

则getIdentifier 返回相关的资源标识符。如果没有这样的资源被发现,则返回0。 (0不是有效的资源ID。)

在情况下,如果这种方法失败,其他我们想看看是否一定的物理键是在设备上present像首页通常构成导航栏。

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.

解决方案

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.

这篇关于如何检测在Android设备上可用软菜单键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:15