判断手机有没有虚拟按键(NavigationBar)?
··· // 判断设备是否有返回键、菜单键来确定是否有 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上,并不能检测出来。
你试了嘛
我试了,hasMenuKey 返回的是false, hasBackKey 返回的是true 去掉判断hasBackKey 就正常了。
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 测试显示不对,是不是逻辑写反了啊。 贴了一下输出: 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
我在Nexus 5上测试
KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK) 返回true。
有什么判断是否有虚拟键的好方式么?求告知。

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