不依赖activity的全局对话框的实现

来源:互联网 发布:办公软件基础教程 编辑:程序博客网 时间:2024/05/16 05:50

实现不依赖于Activity的全局对话框有三种方式:

第一个方法利用系统弹出dialog

在alter.show()语句前加入:

alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  
然后在AndroidManifest.xml中加入权限:Android.permission.SYSTEM_ALERT_WINDOW

第二个方法是获取WindowManager,直接添加view

wmParams = new WindowManager.LayoutParams();  //获取的是WindowManagerImpl.CompatModeWrapper  mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);    //设置window type  wmParams.type = LayoutParams.TYPE_PHONE;      //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)  wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;        //调整悬浮窗显示的停靠位置为左侧置顶  wmParams.gravity = Gravity.LEFT | Gravity.TOP;         // 以屏幕左上角为原点,设置xy初始值,相对于gravity  wmParams.x = 0;  wmParams.y = 0;  //设置悬浮窗口长宽数据    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  LayoutInflater inflater = LayoutInflater.from(getApplication());  //获取浮动窗口视图所在布局  mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);  //添加mFloatLayout  mWindowManager.addView(mFloatLayout, wmParams); 

第三个方法是使用一个透明的activity当背景,在该Activity上弹出对话框

这种比较简单,就相当于常规显示对话框

在项目中遇到同一账号异地登录顶替的对话框出现 第一次可以弹出 第二次弹出报错 所以看到此文章

1 0