Android 禁止下拉通知栏
16lz
2021-01-23
使用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); }}
更多相关文章
- Android沉浸式通知栏的一个开源库SystemBarTint简单使用,android
- Android 购物车加减功能的实现代码
- android 我的功能模块模板布局之一
- android LayoutInflater.inflate()的参数及其功能
- Android几个常用功能代码(校园助手开发笔记)
- Android 通知之 Notification
- Android 发送通知 notification
- android GPS JAVA 获取GPS功能是否禁用
- Android 双击返回键退出功能的实现