android弹出对话框,遇到permission denied for this window type

调试极光推送,在收到推送的时候弹出对话框,在有的机型上报错
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@24a537cb -- permission denied for this window type

相关代码是:

AlertDialog dialog = null;
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("提示");
            builder.setMessage(MyReceiver.message);
            builder.setCancelable(false);
            builder.setPositiveButton("知道了!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (dialog != null) {
                        dialog.dismiss();
                    }

                }
            });
            dialog = builder.create();

            dialog.getWindow()
                    .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

            dialog.show();

解决方法是在AndroidManifest.xml中加入

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" /> 

你可能感兴趣的:(开发备忘)