Android: Disable and enable to pulling the status bar programmatically for API 19

前端 未结 2 448
傲寒
傲寒 2021-01-07 13:44

I already done do to hide the status bar, but unfortunately I didn\'t found the way to display it back once its already hide. I did so many workaround but still not success,

相关标签:
2条回答
  • 2021-01-07 14:21

    I think permanently disable the status bar is difficult. I am also working on the same concept and did lots of R&D and found that below code can be useful. if the user tries to expand the status bar then within a sec it will pull back it and it will work on oreo as well. I have tried on different OS.

    public class BlockStatusBar { 
    Context context
    // To keep track of activity's window focus
        boolean currentFocus;
        // To keep track of activity's foreground/background status
        boolean isPaused;
    
        public Handler collapseNotificationHandler;
        Method collapseStatusBar = null;
    
        public BlockStatusBar(Context context, boolean isPaused) {
            this.context = context;
            this.isPaused = isPaused;
            collapseNow();
        }
    
        public void collapseNow() {
    
    
            // Initialize 'collapseNotificationHandler'
            if (collapseNotificationHandler == null) {
                collapseNotificationHandler = new Handler();
    
            }
    
            // If window focus has been lost && activity is not in a paused state
            // Its a valid check because showing of notification panel
            // steals the focus from current activity's window, but does not
            // 'pause' the activity
            if (!currentFocus && !isPaused) {
    
                Runnable myRunnable = new Runnable() {
                    public void run() {
                        // do something
                        try {
    
                            // Use reflection to trigger a method from 'StatusBarManager'
                            Object statusBarService = context.getSystemService("statusbar");
                            Class<?> statusBarManager = null;
    
                            try {
                                statusBarManager = Class.forName("android.app.StatusBarManager");
                            } catch (ClassNotFoundException e) {
                                Log.e(LOG_TAG, "" + e.getMessage());
                            }
    
                            try {
    
                                // Prior to API 17, the method to call is 'collapse()'
                                // API 17 onwards, the method to call is `collapsePanels()`
                                if (Build.VERSION.SDK_INT > 16) {
                                    collapseStatusBar = statusBarManager.getMethod("collapsePanels");
                                } else {
                                    collapseStatusBar = statusBarManager.getMethod("collapse");
                                }
                            } catch (NoSuchMethodException e) {
                                Log.e(LOG_TAG, "" + e.getMessage());
                            }
    
                            collapseStatusBar.setAccessible(true);
    
                            try {
                                collapseStatusBar.invoke(statusBarService);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
    
                            // Check if the window focus has been returned
                            // If it hasn'kioskthread been returned, post this Runnable again
                            // Currently, the delay is 100 ms. You can change this
                            // value to suit your needs.
                            if (!currentFocus && !isPaused) {
                                collapseNotificationHandler.postDelayed(this, 100L);
    
                            }
                            if (!currentFocus && isPaused) {
                                collapseNotificationHandler.removeCallbacksAndMessages(null);
                            }
                        } catch (Exception e) {
                            Log.e("MSG", "" + e.getMessage());
                        }
                    }
                };
                // Post a Runnable with some delay - currently set to 300 ms
                collapseNotificationHandler.postDelayed(myRunnable, 1L);
    
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 14:39

    For Hiding Status Bar on 4.1 or Higher use

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    

    OR

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    To make Status Bar Visible

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.show();
    

    OR

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
    

    Refer Immersve View

    android Exit from full screen mode

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