Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

How to detect soft menu key available in android device?

I would like to check menu key presence in the device in android application. To achieve that i used following code to detect weather device is having hardware menu or not and it is working fine

!ViewConfiguration.get(ScsCommander.getInstance().getApplicationContext()).hasPermanentMenuKey()

But i did not find a logic to find weather the device is having soft menu present or not.

Please suggest me is there any way to detect soft menu is available or not in the device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...