0

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 alarm
  • ActivityBis the one in charge of showing the AlertDialog 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.

Community
  • 1
  • 1
Eleanore
  • 1,750
  • 3
  • 16
  • 33

1 Answers1

0

You are using PendingIntent.getService but should getActivity

UPD About AlertDialog, you could use transparent activity and still open AlertDialog.

Volodymyr Lykhonis
  • 2,936
  • 2
  • 17
  • 13
  • I have tried switching with this second method, but it seems again mute, unfortunately. Maybe handling the thing in the onCreate method is wrong? – Eleanore Aug 17 '13 at 15:24
  • Why you call `finish` onCreate? – Volodymyr Lykhonis Aug 17 '13 at 15:55
  • All the code that the activity has to execute is in the onCreate method. Apart from this, I tried to debug the code: it never happens that the execution reaches that method. – Eleanore Aug 17 '13 at 20:07
  • Well, we are just making assumptions here. It is hard to say. Did you define activities in android manifest? How do you know it never reaches onCreate of the activity? – Volodymyr Lykhonis Aug 17 '13 at 22:56