Android--在非activity弹出Dialog对话框

需要在adapter类监听button点击事件弹出需要弹出一个系统级对话框,也就是这个对话框不论是在哪个活动,都可以弹出这个对话框。所以要设置对话框的属性为:

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT  

需要加入权限:

  

Android6.0以上提权限,需要写代码跳转到设置界面让用户手动给“显示悬浮框”权限,跳转参考:https://blog.csdn.net/chaoyu168/article/details/80280200

简单实现:

private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    AlertDialog.Builder builder = new AlertDialog.Builder(MyApplication.getContext());
                    builder.setTitle("温馨提示:")/*设置dialog的title*/
                            .setMessage("已经完成")/*设置dialog的内容*/
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {/*设置dialog确认按钮的点击事件*/
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    AlertDialog alertDialog = builder.create();
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    alertDialog.show();
                    break;

你可能感兴趣的:(Android,Android开发)