【问题标题】:android detect Samsung Galaxy S8 navigation bar hide or show programmaticallyandroid以编程方式检测三星Galaxy S8导航栏隐藏或显示
【发布时间】:2017-09-08 01:53:16
【问题描述】:

在三星 S8 等某些设备上,导航栏可以隐藏或显示,这在某些情况下是个问题。 Samsung S8's navigation bar can be hide or show by click left bottom button

即使在 Android 源代码中,我也没有找到直接的方法来确定。

我搜索了一些问题,例如 A good solution to check for navigation bar ,但没有帮助。

非常感谢任何帮助。

【问题讨论】:

  • 你找到答案了吗?我遇到了同样的问题
  • @TUSHAR 不,我仍然找不到好的解决方案。我认为 Android 应该让它标准化。
  • 随着越来越多的全面屏手机出现,不仅仅是三星Galaxy S8/Note8,还有小米、华为等。如果Android Sources不尽快标准化,这可能会成为一个严重的问题.

标签: android android-navigation-bar


【解决方案1】:

首先感谢原作者:https://www.jianshu.com/p/ddfbabd614b6

对于三星手机(即 S8、S9 等),您可以通过收听三星事件来检测导航栏是否正在显示。

private static final String SAMSUNG_NAVIGATION_EVENT = "navigationbar_hide_bar_enabled";

然后听听这个事件,做你的事:

private void checkNavigationBar() {
    if (isSamsungVersionNougat()) { // check if Samsung phones
        // detect navigation bar
        try {
            // there are navigation bar
            if (Settings.Global.getInt(activity.getContentResolver(), SAMSUNG_NAVIGATION_EVENT) == 0) {
                // Your code
                // example: galleryViewModel.navigationBarHeight.set(getNavigationBarHeight());
            } else { // there are no navigation bar
                // Your code
                // example: galleryViewModel.navigationBarHeight.set(0);
            }
        } catch (Exception ignored) {
        }
        barHideEnableObserver = new BarHideEnableObserver(new Handler());
        activity.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(SAMSUNG_NAVIGATION_EVENT),
                true, barHideEnableObserver);
    } else {
        galleryViewModel.navigationBarHeight.set(getNavigationBarHeight());
    }
}

【讨论】:

  • 为我工作,这是我们在具有可折叠栏的三星手机上查找全屏窗口高度的唯一解决方案
  • 知道如何在其他制造商的设备上检测到这一点吗?
  • 对于有相同问题的 xamarin 开发人员check this
  • 好的,这就是它的来源:jianshu.com/p/ddfbabd614b6
【解决方案2】:

使用这种方法,对我有用。确保首先渲染视图以确保 getHeight() 不返回 0。还要确保您使用的 rootView 是为了占据整个屏幕。

public static boolean hasNavBar (Activity activity, View rootView) {
    if (activity == null || rootView == null)
        return true;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return true;

    Display d = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics realDisplayMetrics = new DisplayMetrics();
    d.getRealMetrics(realDisplayMetrics);

    int viewHeight = rootView.getHeight();
    if (viewHeight == 0)
        return true;

    int realHeight = realDisplayMetrics.heightPixels;
    return realHeight != viewHeight;
}

【讨论】:

  • 在活动窗口设置此标志时不起作用:WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 2018-06-26
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
相关资源
最近更新 更多