简体   繁体   中英

How to show alertdialog from another class and handle it in Current Activity(Android)

I am sending notification on certain occurrence and notify user via notification if app is in background and via alert if user is in foreground . Now I wonder how to show alert dialog from my notification class and handle that dialog in Current Activity .

Please Guide me on this. Any help will be appreciated.

This will Work by sending context to another activity

public class Message {

 public static void alert_msg(Context context, String title, String message) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Set Dialog Title
    alertDialog.setTitle(title);

    // Set Dialog Message
    alertDialog.setMessage(message);


    // Set OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    // Show Alert Message
    alertDialog.show();
 }
}

call using this

  Message.alert_msg(MainActivity.this,"Title","Your Message Here"); 

You will need to set up an Intent and a Broadcast Receiver. That will allow you to broadcast and intent from your notification activity and if your app is in the foreground, then the broadcast receiver configured in the app can pick it up and display a dialog.

http://developer.android.com/reference/android/content/BroadcastReceiver.html http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

-> by passing DialogInterface.OnClickListener() object in parameters and implementing it in Activity class

AlertDialogs alertdialog;

    alertdialog.SingleSelectDialog("title",otherParameters,new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          int x=alertdialog.getAmount();

                        }
                    });

-> in class AlertDialogs

        public void SingleSelectWithImage(String head,otherParameters, DialogInterface.OnClickListener pressok) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.MyDialogTheme);
        dialogBuilder.setTitle(head);

              amount=bla_bla;    
               //do anything
        dialogBuilder.setPositiveButton("ok", pressok);
        dialogBuilder.setNegativeButton("Cancel", null);
        dialogBuilder.show();        

}

-> getter method

 public int getAmount() {
        return amount;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM