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

发布于 2024-12-16 22:19:11 字数 55 浏览 5 评论 0原文

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

I have a service that should check periodically visibility of status bar, when some top activity is (or not) in fullscreen mode.
Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿嘿嘿 2024-12-23 22:19:11

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

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 。

Finally I have discovered how to check if statusbar is visible or not. Its some kind of hack, but it works for me. I created that method in my Service:

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;
                }
            }
        });

    }

where widthS and heightS our global screen size;
Here I just compared invisible helper window height to screen height and make decision if status bar is visible. And do not forget to remove helperWnd in onDestroy of your Service.

极度宠爱 2024-12-23 22:19:11

你好,如果你尝试这个提供 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

hello and if you try this code that provides android as good practice

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.
        }
    }
});

I leave the link of the documentation: https://developer.android.com/training/system-ui/visibility#java

眼波传意 2024-12-23 22:19:11
public boolean isStatusBarVisible() {
    Rect rectangle = new Rect();
    Window window = getActivity().getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
    int statusBarHeight = rectangle.top;
    return statusBarHeight != 0;
}
public boolean isStatusBarVisible() {
    Rect rectangle = new Rect();
    Window window = getActivity().getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
    int statusBarHeight = rectangle.top;
    return statusBarHeight != 0;
}
九局 2024-12-23 22:19:11

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

Not really. If it is your activity in the foreground, your activity could tell the service if it is using a theme that hides the status bar. You have no way of determining this independently from the service though, let alone for third-party activities rather than your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文