Android: Programmatically detect if device has har

2019-01-03 14:32发布

I'm currently struggling with this issue. I need to check if the device, where the app is installed, has a hardware menu key. Because it is not existing on some devices like Galaxy Nexus, I'm showing it directly in the UI in this case.

I have already looked at PackageManager.hasSystemFeature(), but didn't find anything useful there.

Has anyone already done this?

6条回答
【Aperson】
2楼-- · 2019-01-03 14:48

keysoft qualifier is used to detect a hardware keyboard not the navigation bar.

This article solves it:

Check for navigation bar

查看更多
再贱就再见
3楼-- · 2019-01-03 14:51

Even on devices running Honeycomb and later, the system will supply a “Menu button” for apps written for 2.x versions of Android. Only it’s called the “overflow menu”. So there’s no point checking whether there will be such a button or not—it will be there if it’s needed.

As a general guideline, you should check for specific functionality, not look at system/API version numbers. Use the ActionBar class if it’s available, otherwise fallback to the 2.x options menu.

Have you looked at Google’s action-bar tutorial? That makes it clearer what you should be doing.

查看更多
【Aperson】
4楼-- · 2019-01-03 14:52
if(Build.VERSION.SDK_INT <= 10 || (Build.VERSION.SDK_INT >= 14 &&    
                              ViewConfiguration.get(this).hasPermanentMenuKey()))
{
   // menu key is present
}
else
{
  //No menu key
}
查看更多
Lonely孤独者°
5楼-- · 2019-01-03 14:53
ViewConfiguration.get(context).hasPermanentMenuKey()

See ViewConfiguration#hasPermanentMenuKey() for more information. Note that this is only available for API level 14+ (Android 4.0 Ice Cream Sandwich or newer).

查看更多
Ridiculous、
6楼-- · 2019-01-03 15:05

I think a possible and better solution is to add a own actionbar. So every device can see it and you don't have to check hardware configuration or Api version.

查看更多
干净又极端
7楼-- · 2019-01-03 15:10

If you want a resource qualifier, which might be the case since you want to differentiate UIs, use keyssoft resource qualifier.

查看更多
登录 后发表回答