Question

The problem I am having is that if I set up c2 a calendar time for lets say 24 hours from now that should display a notification although it all works if I perform a reboot on my phone then the notification will no longer be displayed at the given time.

I am using the following code in my class to schedule an event based off a certain Calendar time that I set using c2.

 Intent myIntent = new Intent(Appointments.this, MyAlarmService.class);
      pendingIntent = PendingIntent.getService(Appointments.this, 0, myIntent, 0);
               AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
               alarmManager.set(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(), pendingIntent);

The service class I have once activated from the pending intent based off what ever c2 is set too will do the following in MyAlaramService.class

public void onStart(Intent intent, int startId) {

    // TODO Auto-generated method stub

    super.onStart(intent, startId);
    Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();


    NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),Appointments.class);

       Notification notification = new Notification(R.drawable.icon,"Appointment Reminder For Tommorow!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "myCF Reminder", "Appointment Tommorw!", pendingNotificationIntent);

       mManager.notify(0, notification);
 this.stopSelf();


}
Was it helpful?

Solution

if I perform a reboot on my phone then the notification will no longer be displayed at the given time

You will need to get control after a reboot, such as via a BOOT_COMPLETED BroadcastReceiver, and re-establish your AlarmManager event schedule.

This sample project demonstrates the basics, though it's a pretty trivial example.

Also, please note that using RTC_WAKEUP with a service PendingIntent is unreliable. If you want to use a _WAKEUP alarm, please use WakefulBroadcastReceiver or my WakefulIntentService. Here are versions of the same sample that I linked to above that use WakefulIntentService and WakefulBroadcastReceiver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top