Is there a way to check the visibility of the status bar?

后端 未结 4 1015
鱼传尺愫
鱼传尺愫 2020-12-29 13:25

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?

相关标签:
4条回答
  • 2020-12-29 13:47

    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.

    0 讨论(0)
  • 2020-12-29 13:50
    public boolean isStatusBarVisible() {
        Rect rectangle = new Rect();
        Window window = getActivity().getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
        int statusBarHeight = rectangle.top;
        return statusBarHeight != 0;
    }
    
    0 讨论(0)
  • 2020-12-29 13:58

    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

    0 讨论(0)
  • 2020-12-29 14:03

    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.

    0 讨论(0)
提交回复
热议问题