您的位置:首页 > 移动开发 > Android开发

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的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android