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?
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:
Game) and your Dialog pops up every 15 minutes, this will be very
annoying for him.
suited) ways to notify him that something has been executed.
Better suited ways
In fact, you can create/show a
Toastfrom anBroadcastReceiver. ThisToastwill 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 passedContext-Object from theonReceive-method).The Notification will also be shown when the user is not “in your application” and is IMO the best solution to this problem.