Android:禁用并启用以编程方式为API 19提取状态栏

时间:2018-02-14 08:31:52

标签: android statusbar

我已经完成了隐藏状态栏的操作,但遗憾的是,一旦隐藏状态栏,我就没有找到显示它的方法。我做了很多解决方法,但仍然没有成功,herehere

活动

 WindowManager manager = ((WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE));

 WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
 localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
 localLayoutParams.gravity = Gravity.TOP;
 localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

 // this is to enable the notification to receive touch events
 WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

 // Draws over status bar
 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

 localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
 localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
 localLayoutParams.format = PixelFormat.TRANSPARENT;

 customViewGroup view = new customViewGroup(this);
 manager.addView(view, localLayoutParams);

customViewGroup类

class customViewGroup extends ViewGroup {

    public customViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

2 个答案:

答案 0 :(得分:8)

用于隐藏4.1或更高版本的状态栏

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();

或者

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

使状态栏可见

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();

或者

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

Refer Immersve View

android Exit from full screen mode

答案 1 :(得分:1)

我认为永久禁用状态栏很困难。我也在研究相同的概念,并做了很多研发工作,发现下面的代码可能有用。如果用户尝试扩展状态栏,则将在几秒钟内将其撤回,并且也将在oreo上运行。我曾尝试过使用其他操作系统。

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);

        }
    }
}