2

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Ponting
  • 2,248
  • 8
  • 33
  • 61
  • here is my Answer ! , http://stackoverflow.com/questions/3393908/how-to-get-any-identifier-of-the-topmost-activity/26308339#26308339 – Mojtaba Yeganeh Oct 10 '14 at 21:34

3 Answers3

4

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"); 
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Rohan Kadu
  • 1,311
  • 2
  • 12
  • 22
  • Ok.Thanks .But I got error on MainActivity.this.How can I pass My activity to Message.alert_msg argument? – Ponting Sep 24 '13 at 14:26
  • you have to just pass context to that method...placed in come another class . – Rohan Kadu Sep 24 '13 at 14:30
  • Ok.I did as you said but it gave me error as ARCA caught a badtoken exception for my bundle on line of alertDialog.show(). – Ponting Sep 24 '13 at 17:17
1

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

nomachinez
  • 521
  • 4
  • 6
  • 1
    No problem! If you feel a little lost in the whole broadcast receiver thing, let us know and if you provide some code maybe we can help. Once you understand it, they are very, very easy. – nomachinez Sep 24 '13 at 14:34
  • Yeah It seems like very tough to understand at first glance. – Ponting Sep 24 '13 at 14:35
0

-> 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;
    }