Displaying alert dialog box in all the screens

2019-04-18 01:45发布

I am developing an application which will display an Alertdialog box when it gets an event. Currently the alert will come only in that particular activity. I need to get this alert on all the screens ( eg. home screen, message screen, etc )except I am in a call.

Please provide a solution for this.

4条回答
手持菜刀,她持情操
2楼-- · 2019-04-18 02:34

Create one class and make one constructor in it . Pass the Activity context to constructor. Now make one function and put code of Alert Dialog in it.

Now whenever you want that dialog you can just call it with crate object of that class and call function of alert Dialog.

查看更多
【Aperson】
3楼-- · 2019-04-18 02:41

You can create a AlartMessage.java file in util package where put this static method.

public static void showMessage(final Context c, final String title,
        final String s) {
    final AlertDialog.Builder aBuilder = new AlertDialog.Builder(c);
    aBuilder.setTitle(title);
    // aBuilder.setIcon(R.drawable.icon);
    aBuilder.setMessage(s);

    aBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            dialog.dismiss();
        }

    });

    aBuilder.show();
}
查看更多
你好瞎i
4楼-- · 2019-04-18 02:42

In Utils class, make this method :

public static AlertDialog showAlertWithListeners(Context context, String title, String message, String textPositive,
                                          String textNegative,
                                          DialogInterface.OnClickListener positiveButtonClickListener,
                                          DialogInterface.OnClickListener negativeButtonClickListener) {
    if (textNegative != null)
        return new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(textPositive, positiveButtonClickListener)
                .setNegativeButton(textPositive, negativeButtonClickListener)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    else
        return new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(textPositive, positiveButtonClickListener)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();

}

For example, in class in which you need to show alert :

DialogInterface.OnClickListener positiveButtonClickListener = new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            startActivity(new Intent(context, SignInActivity.class));
                                            SignUpActivity.this.finish();
                                        }
                                    };
AlertDialog successDialog = Utils.showAlertWithListeners(context, getResources().getString(R.string.success),
                                            signUpDetailResponse.message, getResources().getString(R.string.ok), null,
                                            positiveButtonClickListener, null);
successDialog.show();
查看更多
Viruses.
5楼-- · 2019-04-18 02:45

Create some Utils class with static method that will takes Context as an argument and builds whole dialog.

Edit:

public class Utils {

    public static AlertDialog getDialog(Context context) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder
            .setTitle("title")
            .create()
            ;

    }

}

And call it in every place you need:

Utils.getDialog(context).show();
查看更多
登录 后发表回答