android中不依赖activity的dialog弹窗的实现

    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中声明了
也不会生效,比如我遇到的魅族pro5就不会生效.
而toast级别的已经足够我们使用了,而且是不再依赖于activity的.

你可能感兴趣的:(学习笔记)