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

情到浓时终转凉″ 提交于 2019-12-18 11:57:43

问题


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?


回答1:


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.




回答2:


public boolean isStatusBarVisible() {
    Rect rectangle = new Rect();
    Window window = getActivity().getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
    int statusBarHeight = rectangle.top;
    return statusBarHeight != 0;
}



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/8200838/is-there-a-way-to-check-the-visibility-of-the-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!