android中不依赖activity的dialog弹窗的实现
2016-07-13 16:14
381 查看
private void showUpdateSuccessDialog(){ final WindowManager wm = (WindowManager) AppContext.context.getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams para = new WindowManager.LayoutParams(); para.height = -2;//WRAP_CONTENT para.width = -2;//WRAP_CONTENT para.format = 1; para.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; para.gravity = Gravity.CENTER; para.type = WindowManager.LayoutParams.TYPE_TOAST; final View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout, null); TextView tvDlgTiTle = (TextView) contentView.findViewById(R.id.tv_title_dialog); TextView tvDlgSubTitle = (TextView) contentView.findViewById(R.id.tv_subtitle_dialog); TextView tvDlgBtn = (TextView) contentView.findViewById(R.id.tv_dialog_btn); tvDlgTiTle.setText("更新成功"); tvDlgSubTitle.setText("固件将会重启"); tvDlgBtn.setText("固件重启将会持续几分钟..."); tvDlgBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { wm.removeView(contentView); } }); wm.addView(contentView, para); }
其中的关键就是
para.type = WindowManager.LayoutParams.TYPE_TOAST;
这段代码,即设置一个window级别与toast相同的弹窗.一般网上推荐使用的是
para.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;但在某些手机系统上即便在manifest中声明了
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>也不会生效,比如我遇到的魅族pro5就不会生效.
而toast级别的已经足够我们使用了,而且是不再依赖于activity的.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories