Respect for the original, respect for the author, reproduced please indicate the source:
http://blog.csdn.net/lnb333666/article/details/41821149
There is no reliable way to check if there is a navigation bar on the device. You can use Keycharactermap.devicehaskey to check if there are some physical keys on the device, such as menu keys, return keys, and home keys. Then we can determine whether there is a navigationbar by physical key or not (usually the physical keys, navigationbar coexist on the phone).
[Java]View Plaincopyprint?
- Public static int getnavigationbarheight (activity activity) {
- Resources resources = Activity.getresources ();
- int resourceId = Resources.getidentifier ("Navigation_bar_height",
- "Dimen", "Android");
- //Get the height of Navigationbar
- int height = resources.getdimensionpixelsize (resourceId);
- return height;
- }
The above code, in the vast majority of cases can be obtained to the height of the navigationbar. So some people want to use this height to judge whether there is navigationbar is not possible. Of course, the 4.0 version below is needless to say. Confirm a question, Navigationbar is 4.0 above only?
Because the device has a physical key, you can still have a navigation bar. Any device that runs a custom ROM will set an option to disable the physical key and add a navigation bar. Look at the API:
Viewconfiguration.get (context context). Haspermanentmenukey () has a description: report if the device has a permanent menu key avail Able to the user (report if the device has a permanent menu that is primarily available to users).
Description of the Android.view.KeyCharacterMap.deviceHasKey (int keycode): Queries The framework about whether any physical keys exist on The any keyboard attached to the device that is capable of producing the given key code (the query framework exists for any physical keyboard that is connected to the production of any keyboard to give key codes 's ability. ).
So the solution is:
[Java]View Plaincopyprint?
- @SuppressLint ("Newapi")
- public Static Boolean Checkdevicehasnavigationbar (Context activity) {
- determine if there is a navigation bar by judging whether the device has a return key, a menu key (not a virtual key, or a button outside the phone screen)
- Boolean hasmenukey = Viewconfiguration.get (activity)
- . Haspermanentmenukey ();
- Boolean hasbackkey = Keycharactermap
- . Devicehaskey (Keyevent.keycode_back);
- if (!hasmenukey &&!hasbackkey) {
- //Do whatever you need to do, this device has a navigation bar
- return true;
- }
- return false;
- }
Android Check if the device has a navigation bar Navigationbar