0

I am making an alarm app. This app calls an intent when the alarm is active (active by the calender). When the alarm or calendar goes off, I want to call an activity, which is hello.class. This class is mentioned in the pending intent which gets called by the calender. When the calender gets called and the pending intent should be dealt with, NOTHING at all happens. Any ideas on what I am doing wrong?

My code is below: The line labeled "ABC" by a comment is where the hello.class is mentioned

Intent myIntent = new Intent(AndroidAlarmService.this, Hello.class); // ABC


pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 5);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

1 Answers1

1

You must add FLAG_ACTIVITY_NEW_TASK to your intent because the activity will be launched outside of the context of an existing activity.

The documentation says so.

Just add

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

below Intent myIntent = new Intent...

And change PendingIntent.getService(...) to PendingIntent.getActivity(...)

Bojan Radivojevic
  • 717
  • 3
  • 12
  • 23