1

How do i show an AlertDialog from receiver class. My receiver class receives by time of my Alarm using AlarmManager. And, my alertdialog can show if my application is not opened also.

How can i achieve this? Thanks.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Do you place any buttons in your alertdialog. Or just showing some details only. – Praveenkumar Mar 12 '12 at 13:26
  • Hi, again you thanks for your response. I just shows some details to alertdialog. Not any buttons. – user1258711 Mar 12 '12 at 13:29
  • You can, check out this stackoverflow thread http://stackoverflow.com/questions/3835160/how-can-i-display-a-dialog-from-an-android-broadcast-receiver – Soham Mar 12 '12 at 13:38

2 Answers2

2

There is not possible to create AlertDialog from Broadcast Receiver. But there is one way to accomplish this task.

  1. Create a activity and set the theme as a dialog.
  2. OnReceive() method of your Broadcast Receiver start the activity which you had create in 1st step
  3. You have to set the flag Intent.FLAG_ACTIVITY_NEW_TASK to start the activity from the broadcast receiver.

So you code will looks like below

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, yourDialogActivity.class);
    i .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i);
}
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
0

Yes, you can do like this. Just start one activity class from your receiver using Intent and in that activity class just use the code from this Blog

Change that class with whatever you needs for AlertDialog. This will display the when your application closed also. Hope this helps you.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173