安卓开发之禁止下拉通知栏(悬浮窗口解决方案4.2系统 6.0以上)

  //禁止下拉通知栏(悬浮窗口解决方案,安卓4.2系统)
    public static void preventStatusBarExpansion(Context context) {

        WindowManager manager = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        Activity activity = (Activity) context;
        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;
        int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int result = 0;
        if (resId > 0) {
            result = activity.getResources().getDimensionPixelSize(resId);

        }

        localLayoutParams.height = result;

        localLayoutParams.format = PixelFormat.TRANSPARENT;
        View view = new customViewGroup(context);

        manager.addView(view, localLayoutParams);

    }

    public static 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) {
            Log.v("customViewGroup", "**********Intercepted");
            return true;
        }
    }
//    其次:在onWindowFocusChanged,onResume,onPause加入判断;
    public void onWindowFocusChanged(boolean hasFocus) {
        currentFocus = hasFocus;
        if (!hasFocus) {

            collapseNow();

        }
        super.onWindowFocusChanged(hasFocus);

    }

    protected void onResume() {
        isPaused = false;
        super.onResume();

    }
    protected void onPause() {
        isPaused = true;
        super.onPause();

    }

    private void collapseNow() {

        if (collapseNotificationHandler == null) {

            collapseNotificationHandler = new Handler();

        }
        if (!currentFocus && !isPaused) {
            collapseNotificationHandler.postDelayed(new Runnable() {
                @Override

                public void run() {

                    Object statusBarService = getSystemService("statusbar");

                    Class<?> statusBarManager = null;

                    try {

                        statusBarManager = Class.forName("android.app.StatusBarManager");

                    } catch (ClassNotFoundException e) {

                        e.printStackTrace();

                    }
                   //这边的method方法导入的包是import java.lang.reflect.Method;

                    Method collapseStatusBar = null;

                    try {

                        if (Build.VERSION.SDK_INT > 16) {

                            collapseStatusBar = statusBarManager.getMethod("collapsePanels");

                        } else {

                            collapseStatusBar = statusBarManager.getMethod("collapse");

                        }

                    } catch (NoSuchMethodException e) {

                    }

                    collapseStatusBar.setAccessible(true);

                    try {

                        collapseStatusBar.invoke(statusBarService);

                    } catch (IllegalArgumentException e) {

                        e.printStackTrace();

                    } catch (IllegalAccessException e) {

                        e.printStackTrace();

                    } catch (InvocationTargetException e) {

                        e.printStackTrace();

                    }
                    if (!currentFocus && !isPaused) {

                        collapseNotificationHandler.postDelayed(this, 10L);

                    }
                }

            }, 10L);

        }

    }

最后:在自己的activity中加上:

private Handler collapseNotificationHandler;

private boolean currentFocus;

private boolean isPaused;

最后的最后:别忘记了在AndroidManifest.xml加上权限

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

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

至此,你编译出来的apk就可以屏蔽系统的通知栏下拉了。

希望以上方法可以帮助需要的人!
//安卓6.0及以上
//创建一个类
public class CustomViewGroup extends ViewGroup {
    public CustomViewGroup(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}

 //禁止下拉方法

  private CustomViewGroup view;
  WindowManager manager;

  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) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;
        view = new CustomViewGroup(this);
        manager.addView(view, localLayoutParams);
    }

//在onCreate中调用
  if (! Settings.canDrawOverlays(MainActivity.this)) {
            Intent newintent = new Intent(Settings.
                    ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivity(newintent); }

        prohibitDropDown();

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amoour

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值