2021-05-07 android 禁止状态栏下拉

实现思路是获取其他应用上层悬浮窗的权限,然后在状态栏的位置写一个状态栏高度的透明view
代码如下

 CustomViewGroup view;
    WindowManager manager;

    /**
     * activity启动时调用禁止下拉
     */
    private void prohibitDropDown() {
        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 recieve 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) (30 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;
        view = new CustomViewGroup(this);
        manager.addView(view, localLayoutParams);
    }


    /**
     * activity销毁时允许下拉,不然程序不大退,怎么都无法下拉状态栏
     */
    private void allowDropDown() {
        manager.removeView(view);
    }

6.0以下的直接在AndroidManifest.xml里加


6.0以后在AndroidManifest.xml里加了权限以后还要要自己跳转设置

  Intent intent1 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent1, 11);

你可能感兴趣的:(2021-05-07 android 禁止状态栏下拉)