user1258711
user1258711

Reputation: 23

Showing an AlertDialog from Receiver

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.

Upvotes: 1

Views: 986

Answers (2)

Dharmendra
Dharmendra

Reputation: 33996

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

Upvotes: 2

Praveenkumar
Praveenkumar

Reputation: 24476

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.

Upvotes: 0

Related Questions