There is no reliable/clean way to check if soft menu (aka Navigation bar) is present or not!
You may try using below code (not tested on all devices and not a reliable solution anyways):
boolean hasNavBar(Context context) {
Resources resources = context.getResources();
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
return resources.getBoolean(id);
} else { // Check for keys
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !hasMenuKey && !hasBackKey;
}
}
Here, we are fetching the resource identifier using Resources
class. We are looking for a resource "navigation bar" which is passed as the 1st parameter.
The getIdentifier
returns the associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
In case if this approach fails, in else
we are trying to see check if certain physical keys are present on the device like back
or Home
which usually constitute Navigation bar.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…