In-depthStudy icon indicating copy to clipboard operation
In-depthStudy copied to clipboard

判断手机有没有虚拟按键(NavigationBar)?

Open BaymaxTong opened this issue 9 years ago • 5 comments

··· // 判断设备是否有返回键、菜单键来确定是否有 NavigationBar public static boolean hasNavigationBar(Context context) { boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey(); boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK); if (!hasMenuKey && !hasBackKey) { return true; } return false; } ··· 按照这种做法,在华为手机5X上,并不能检测出来。

BaymaxTong avatar Aug 01 '16 08:08 BaymaxTong

你试了嘛

sunfusheng avatar Aug 02 '16 04:08 sunfusheng

我试了,hasMenuKey 返回的是false, hasBackKey 返回的是true 去掉判断hasBackKey 就正常了。

BaymaxTong avatar Aug 03 '16 12:08 BaymaxTong

private boolean hasNavigationBar(){
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    if (hasBackKey && hasHomeKey) {
        return false;
        // no navigation bar, unless it is enabled in the settings
    } else {
        return true;
        // 99% sure there's a navigation bar
    }
}

可以试试这个 我这边几台手机用这个是能判断的

zzkong avatar Aug 05 '16 01:08 zzkong

@zzkong 测试显示不对,是不是逻辑写反了啊。 贴了一下输出: 08-05 14:20:38.338 2599-2599/io.gank.gank D/bar: hasBackKey: true 08-05 14:20:38.338 2599-2599/io.gank.gank D/bar: hasHomeKey: true 都返回的是true

BaymaxTong avatar Aug 05 '16 06:08 BaymaxTong

我在Nexus 5上测试 KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK) 返回true。

有什么判断是否有虚拟键的好方式么?求告知。 image

------结贴----- https://gist.github.com/ZQiang94/cec97ced06b3a4a8a2dee6ad0a5b6112

ZQiang94 avatar Apr 07 '17 09:04 ZQiang94