有没有办法检查状态栏的可见性?

时间:2011-11-20 10:13:29

标签: android android-layout statusbar

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

4 个答案:

答案 0 :(得分:13)

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

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是我们的全局屏幕尺寸; 在这里,我只是将隐形辅助窗口高度与屏幕高度进行比较,并确定状态栏是否可见。并且不要忘记在服务的onDestroy中删除helperWnd。

答案 1 :(得分:1)

def __init__(self):
    self.variable = 'Old'
    self.change()

def change(self):
    self.variable = 'New'

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您好,如果您尝试使用此代码,可以将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