当前位置: 首页 > >

SYSTEM_ALERT_WINDOW 不同版本权限问题

发布时间:

最*按照网上 写一个系统级别弹窗demo 例如桌面加速小火箭


遇到的问题


Unable to add window android.view.ViewRootImpl$W@4103e4c0 -- permission denied for this window type


原因是:


发现在使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT涉及到window type权限问题


首先配置文件申请权限


在Mainfest.xml中添加window 权限:


?
???


代码中 对不同版本进行判断


//for android 7.0
?mParams.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
?//for android 8.0
// mParams.type=WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;


代码:


final WindowManager.LayoutParams params = mParams;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSPARENT;
//这里针对不同版本进行判断
if (Build.VERSION.SDK_INT >= 26)//8.0
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
else if(Build.VERSION.SDK_INT<23){//6.0
params.type = WindowManager.LayoutParams.TYPE_TOAST;
}else {
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
}
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
params.setTitle("Toast");
params.gravity = Gravity.LEFT + Gravity.TOP;

mRocketView = View.inflate(this, R.layout.rocket_view, null);
//开启动画
final ImageView rocketView = mRocketView.findViewById(R.id.id_img_rocket);
AnimationDrawable animationDrawable = (AnimationDrawable) rocketView.getBackground();
animationDrawable.stop();

//挂到window窗口 (权限为处理好 此处会报错)
mWindowManager.addView(mRocketView, params);

?



友情链接: