Search code examples
javaandroidbroadcastreceiveralarmmanagerreboot

Multiple Alarms Restart After Boot


I'm setting multiple alarms so they can be repeated on specific days. Now I have heard that Android doesn't save the alarms on reboot. I have also read that BroadcastReceiver should be used when BOOT_COMPLETED to reschedule all the alarms.

But how do I tell the BroadcastReceiver to reschedule alarms after reboot if I have 5 alarms per day = around 35 alarms scheduled on different days. Do I need to store them in the database or? How do I store them? Or is the BOOT_COMPLETED all I need? Is there any example for this kind of thing? I couldn't find it.

This is what I currently have for setting the alarms and my simple receiver class. I'm using Service instead of BroadcastReceiver here because I have heard that BR should be used to only process short things and in the future I'll have to use some long sound clips.

   private void setAlarm(){     
            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingintent = PendingIntent.getService(getBaseContext(), 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + sveskupa, pendingintent);
            Toast.makeText(getBaseContext(), "Alarm is set", Toast.LENGTH_LONG).show();
        }

AlarmReceiver class:

public class AlarmReceiver extends Service{

            @Override
            public IBinder onBind(Intent intent) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void onCreate() {
                // TODO Auto-generated method stub
                super.onCreate();
                final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio3);
                MPRadio1.start();
                Toast.makeText(getBaseContext(), "OnCreate", Toast.LENGTH_LONG).show();

            }        

Solution

  • But how do I tell the BroadcastReceiver to reschedule alarms after reboot if I have 5 alarms per day = around 35 alarms scheduled on different days.

    Either the alarm schedule is fixed, unchanging, and baked into your code, or it is not.

    If it is baked into your code, just use that same code from your boot-time BroadcastReceiver to re-establish the alarm schedule.

    Otherwise, the alarm schedule is coming from somewhere, as the alarm schedule is unlikely to have spontaneously been created due to the interaction of cosmic rays with the CPU and memory of the phone. You need to ensure that you have access to that same information after a reboot.

    Do I need to store them in the database or? How do I store them?

    That is up to you.

    I'm using Service instead of BroadcastReceiver here because I have heard that BR should be used to only process short things and in the future I'll have to use some long sound clips.

    Do not use a Service directly from a _WAKEUP-style alarm, as there is no guarantee that your Service will ever get control. Either do not use a _WAKEUP-style alarm, or have the alarm trigger a BroadcastReceiver, which can work with a WakeLock to ensure that your Service gets control and can complete its work.