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

后端 未结 4 1016
鱼传尺愫
鱼传尺愫 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.

提交回复
热议问题