Android系列之系统级别的AlertDialog

AlertDialog 的坑

对话框是安卓开发里面使用的一个很广的组件,我们可以利用这个组件来完成很多的功能,而这篇文章主要讲到的是在开发中使用 AlertDialog 时候遇到的
一个小问题,开发时的代码类似下面这些:


  AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));

  dialogBuilder.setTitle("xx");
  dialogBuilder.setMessage("xxx");

  dialogBuilder.setCancelable(false);


  dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCollection.finishAll();
                Intent intent = new Intent(context, LoginActivity.class);

                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent);
            }
        });

  AlertDialog alertDialog = dialogBuilder.create();
 alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

在 AndroidManifest.xml 文件中还需要配置一个权限:

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

以上的代码是弹出一个系统级别的警告框,上面的那些代码是没有问题的,但是在小米手机上调试时,程序没有报错,但是那个警告框就是
没有弹出来,好吧,找了半天发现,需要在手机上开启这个系统对话框的权限:

©2024 Rayjun    PowerBy Hexo