I am trying to set an alarm in my application. Whenever the alarm is started, an AlertDialog
is shown to the user.
I have created two activities:
ActivityA
is the one in charge of setting the alarmActivityB
is the one in charge of showing theAlertDialog
window to the user
ActivityA
: setting the alarm
The alarm date is stored in the variable alarmDate
of type GregorianCalendar
. I created the alarm using the following code (following the answer here):
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(ActivityB.ALARM_MESSAGE, message);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), pendingIntent);
ActivityB
: handling the request
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String message = intent.getStringExtra(ALARM_MESSAGE);
AlertDialog dialog = new AlertDialog.Builder(this).setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener()...)
.create();
dialog.show();
finish();
}
The problem
I have encountered a problem: ActivityB
is not able to capture the intent. Thus, I think that it is never created and I am missing something on how to start it.
Moreover, I don't think this is the most desirable solution, since I need the alarm to show the AlertDialog
window also in case the application is not running.
Any hint on how to solve these problems?
Thank you.