Dialog pops up in Application

2673 단어 applicationdialog
In the daily development process, it is often necessary to control some information on a global scale. For example, network conditions. Of course, the first thing we think of is to monitor the broadcast in the Application, but because the Context in the Dialog needs an Activity, we can't always succeed. I recently discovered that in Applcation, pop-up Dialog can be implemented.

            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
            builder.setMessage("  ?").setCancelable(false).setPositiveButton(" ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            }).setNeutralButton(" ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }).setNegativeButton(" ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.setCancelable(false);
            ***alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);***
            alert.show();
Pay attention to the second-to-last line of code, and then add one to the Manifest,
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
That's it.

좋은 웹페이지 즐겨찾기