Android 禁止下拉通知栏

使用WindowManager添加View覆盖掉通知栏来实现禁止下拉通知栏功能:


import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class DisableNotiView {

    private WindowManager.LayoutParams mLayoutParam;
    private Context mContext;
    private WindowManager wm;
    private LinearLayout relative;

    public DisableNotiView(Context context) {
        mContext = context;
    }

    public void createView(){
        wm = ((WindowManager)mContext.getApplicationContext()
                .getSystemService("window"));

        int mScreenWidth = wm.getDefaultDisplay().getWidth();
        mLayoutParam = new WindowManager.LayoutParams();
        mLayoutParam.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mLayoutParam.format = PixelFormat.RGBA_8888;
        mLayoutParam.windowAnimations = 16973828;
        mLayoutParam.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |  
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |  
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;  
        mLayoutParam.gravity = Gravity.TOP;
        mLayoutParam.width = mScreenWidth;
        mLayoutParam.height = getStatusBarHeight(mContext);

        LayoutInflater inflater = LayoutInflater.from(mContext.getApplicationContext());
        relative = (LinearLayout)inflater.inflate(R.layout.state_bat_linearlayout, null, false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , getStatusBarHeight(mContext));
        relative.setLayoutParams(params);
        relative.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        wm.addView(relative, mLayoutParam);

    }

    public static int getStatusBarHeight(Context context) {  
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");  
        return context.getResources().getDimensionPixelSize(resourceId);  
    }  

    public void removeView(){
        wm.removeView(relative);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值