Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BaymaxTong opened this issue Aug 1, 2016 · 5 comments
Open

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

BaymaxTong opened this issue Aug 1, 2016 · 5 comments

Comments

@BaymaxTong
Copy link

···
// 判断设备是否有返回键、菜单键来确定是否有 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上,并不能检测出来。

@sunfusheng
Copy link
Owner

你试了嘛

@BaymaxTong
Copy link
Author

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

@zzkong
Copy link

zzkong commented Aug 5, 2016

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

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

@BaymaxTong
Copy link
Author

@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

@ZQiang94
Copy link

ZQiang94 commented Apr 7, 2017

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants