Monday, October 12, 2015

Call AlertDialog in background thread, as well on Main thread.

Make sure to put this permission to manifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

public static void callDialog(Context context){
    AlertDialog.Builder alertDialogBiulder;
    alertDialogBiulder =
            new AlertDialog.Builder(context)
                    .setTitle("Title")
                    .setMessage("Would you like to do certain things?")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //do certain things
                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert);

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

No comments:

Post a Comment