Blocking status bar in top down swiping

2019-07-13 11:27发布

I've a full screen application covering the entire screen, top status bar included. Since a top / down swipe is enabled to show some options to the user, it happens that swiping from top to down, the status bar is showing (as when you want to see notifications and swipe top down). Is there a way to avoid this ?

2条回答
欢心
2楼-- · 2019-07-13 12:09

Use type TYPE_SYSTEM_ERROR for WindowManager.LayoutParams, to create hide impossible fullscreen view. Swipes for show status bar and navigation will be blocked.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMainLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_fullscreen, null);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
        PixelFormat.TRANSLUCENT);
    handleParams.gravity = Gravity.TOP;

    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(mMainLayout, handleParams);
}

And you should be show it after display power on (unlock screen) if needs. Add code:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
查看更多
Bombasti
3楼-- · 2019-07-13 12:17

There are a couple of options. Before the line

super.onCreate(savedInstanceState)

add the following:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

This should completely get rid of the status bar. Basicaly the feature no title removes the top toolbar, and to ensure fullscreen size we are setting fullscreen flags for height and width.

As far as preventing from swiping at all, there is a trick that can be used. Basically place an invisible view at the top once you are in true fullscreen that prevents any swiping. For example,

View disableStatusBarView = new View(context);

WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.FILL_PARENT,
    <height of the status bar>,
    // This allows the view to be displayed over the status bar
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
    // this is to keep button presses going to the background window
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
    // this is to enable the notification to recieve touch events
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
    // Draws over status bar
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
    PixelFormat.TRANSLUCENT);

handleParams.gravity = Gravity.TOP;
context.getWindow().addView(disableStatusBarView, handleParams);

This creates an invisible view over the status bar that will receive the touch events and block the events from reaching the status bar, therefore preventing it to be expanded.

Alternatively, and this works the best, is to override the windowFocusChanged method. Essentially you are NOT preventing the status bar from expanding, but YOU ARE preventing use. Because once expanded, it is closed. I say that this works best because with the invisible view methodology, you may want to consider the dimensions of the screen to really make it effective for all device. So there is more work.

First, declare permission in manifest

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

Then Override onWindowFocusChanged method,

public void onWindowFocusChanged(boolean hasFocus)
{
        try
        {
           if(!hasFocus)
           {
                Object service  = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse .setAccessible(true);
                collapse .invoke(service);
           }
        }
        catch(Exception ex)
        {
        }
}
查看更多
登录 后发表回答