【问题标题】:Is there a way to check the visibility of the status bar?有没有办法检查状态栏的可见性?
【发布时间】:2012-01-02 07:18:02
【问题描述】:

我有一项服务应该定期检查状态栏的可见性,当某些顶级活动处于(或不)处于全屏模式时。 有可能吗?

【问题讨论】:

    标签: android android-layout statusbar


    【解决方案1】:

    最后我发现了如何检查状态栏是否可见。它是某种黑客,但它对我有用。我在我的服务中创建了该方法:

    private void createHelperWnd() {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            final WindowManager.LayoutParams p = new WindowManager.LayoutParams();
            p.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
            p.gravity = Gravity.RIGHT | Gravity.TOP;
            p.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            p.width = 1;
            p.height = LayoutParams.MATCH_PARENT;
            p.format = PixelFormat.TRANSPARENT;
            helperWnd = new View(this); //View helperWnd;
    
            wm.addView(helperWnd, p);
            final ViewTreeObserver vto = helperWnd.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
    
                    if (heightS == helperWnd.getHeight()) {
                        isFullScreen = true;
                    } else {
                        isFullScreen = false;
                    }
                }
            });
    
        }
    

    其中 widthS 和 heightS 是我们的全局屏幕尺寸; 在这里,我只是将不可见的帮助窗口高度与屏幕高度进行了比较,并决定状态栏是否可见。并且不要忘记在您的 Service 的 onDestroy 中删除 helperWnd。

    【讨论】:

    • 我自己想出了一个与您类似的方法,除了覆盖 onSizeChanged... 在我的测试中,我发现我们的方法不适用于所有设备。它适用于所有 API10+ AVD 和 Moto Atrix2,但不适用于 Kindle Fire API10 和 SGS2 API15。在这些设备上,当另一个应用程序全屏时,覆盖不会自动调整大小,也不能在没有 FLAG_LAYOUT_IN_SCREEN 的情况下在状态栏上绘制覆盖。您是否遇到过这种情况和/或找到了更好/替代的解决方案? TIA!
    • @deviant,您如何在平板电脑上获得“heightS”?请给出此代码
    【解决方案2】:

    你好,如果你试试这个提供 android 作为良好实践的代码

    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // Note that system bars will only be "visible" if none of the
            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                // TODO: The system bars are visible. Make any desired
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
            } else {
                // TODO: The system bars are NOT visible. Make any desired
                // adjustments to your UI, such as hiding the action bar or
                // other navigational controls.
            }
        }
    });
    

    我留下文档的链接:https://developer.android.com/training/system-ui/visibility#java

    【讨论】:

      【解决方案3】:
      public boolean isStatusBarVisible() {
          Rect rectangle = new Rect();
          Window window = getActivity().getWindow();
          window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
          int statusBarHeight = rectangle.top;
          return statusBarHeight != 0;
      }
      

      【讨论】:

        【解决方案4】:

        不是真的。如果它是 您的 活动在前台,您的活动可以告诉服务它是否正在使用隐藏状态栏的主题。但是,您无法独立于服务来确定这一点,更不用说第三方活动而不是您自己的活动了。

        【讨论】:

        • 我发现了 getSystemUiVisibility() 方法。文档说这是我需要的。但似乎它不起作用。返回结果总是 0 :(
        • SystemUI 监听器工作得很好,但它们只会在 Honeycomb 及更高版本上告诉您软件按钮何时隐藏(在 Honeycomb 中这永远不会)。
        • @deviant 在我对 getSystemUiVisibility 的实验中,如果应用程序已全屏显示,它不会报告 SystemUI 可见性,只有当另一个应用程序使用了 API11+ 中可用的 setSystemUiVisibility 标志时(尽管仍在试验)。你有其他发现吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-31
        • 2011-10-28
        • 1970-01-01
        • 2015-03-27
        • 2011-02-28
        • 1970-01-01
        相关资源
        最近更新 更多