4

I have used a timer method in an Activity class. In that method I have an intent from Activity class to a BroadcastReceiver class.

This BroadcastReceiver class will call on every 15 minutes at background by using AlarmManager.

When I call the BroadcastReceiver class I would like to raise an AlertDialog.

public void timerMethod(){
    Intent intent = new Intent(Activity.this,
      BroadcastReceiverClass.class
    );

    PendingIntent sender = PendingIntent.getBroadcast(
      QualityCallActivity.this,0, intent, 0
    );

    // We want the alarm to go off 30 seconds from now.
    long firstTime = SystemClock.elapsedRealtime();

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    firstTime, 60*1000, sender);
}

BroadcastReceiverClass.java

public void onReceive(Context context, Intent intent)
{
    dialogMethod();
}

How can I raise an AlertDialog from BroadcastReceiver class from a background process?

Claudio
  • 2,191
  • 24
  • 49
prasad.gai
  • 2,977
  • 10
  • 58
  • 93

3 Answers3

7

If your activity is running when the BroadcastReceiver gets the intent you should be able to use runOnUiThread to run a method that creates an AlertDialog, e.g.:

public void onReceive(Context context, Intent intent)
{
    runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder d = new AlertDialog.Builder(MyActivity.this);
            b.setMessage("This is a dialog from within a BroadcastReceiver");
            b.create().show();
        }
    });

}

This works if you make your BroadcastReceiver an inner class to your Activity.

Joel F
  • 2,591
  • 1
  • 19
  • 17
6

In short: It is not possible.

Only Activity's can create/show dialogs. In fact, this has been asked more then once:

Also, this would give a very bad user-experience:

  • If the user is not in your application (let's say he's playing a Game) and your Dialog pops up every 15 minutes, this will be very annoying for him.
  • If the user is in your application, there are several other (better suited) ways to notify him that something has been executed.

Better suited ways

In fact, you can create/show a Toast from an BroadcastReceiver. This Toast will also bee shown when the user is not "in your application".

Also, you can send a Notification (shown in the Notification-Bar at the top of your screen) from a BroadcastReceiver. A tutorial on how to do this (it does not differ from how you do it in an Activity, except that you use the passed Context-Object from the onReceive-method).

The Notification will also be shown when the user is not "in your application" and is IMO the best solution to this problem.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 1
    While you have good points about whether an `AlertDialog` is the best option, it most certainly **is** possible to show one from a `BroadcastReceiver`. As I point out in my answer, it works if your `BroadcastReceiver` is an inner class to your activity. See my updated answer for the code. – Joel F Aug 29 '11 at 14:41
  • But your solution only works when the `BroadcastReceiver` is an inner class of the Activity and only if the Activity is opened when the receiver gets "called". This feels a little hackie and seams not to be wanted by the Android developers. Instead, using a `Toast` or a `Notification` works fine without these limitations. – Lukas Knuth Aug 29 '11 at 14:58
  • 1
    Like you said, it may not be the best option in many situations. But that doesn't mean there will never be a case where it is the right choice, so it makes sense to know what all your options are. So I just wanted to be clear that it is, in fact, possible, since that **was** his question. – Joel F Aug 29 '11 at 15:03
-1

1) In Activity:

public static Context ctx;

onCreate {
    ctx = this;
}

public void showAlertDialog(Context context, String title, String message) {

    final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    // Setting Dialog Title
    alertDialog.setTitle(title);

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

    // Setting OK Button
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Okay",
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) 
        {
            alertDialog.dismiss();
        }
    });

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

2) In BroadcastReceiver.onReceive:

YourActivity ac= new YourActivity ();
ac.showAlertDialog(YourActivity.ctx, "test", "test");
Claudio
  • 2,191
  • 24
  • 49
PhuocLuong
  • 699
  • 9
  • 18