使用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);
}
}