1

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();

            }        
Bostone
  • 36,858
  • 39
  • 167
  • 227
user1880779
  • 2,038
  • 8
  • 30
  • 40

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Alarm schedule is not fixed since I'm making a School Bell application which will be used by various people. This will be up to the user to choose his own schedule. If the class schedule changes so will alarm have to be changed. If that is the case what would be the simplest way to do it? Thanks for the Service _WAKEUP tip, I guess I'll have to use WakeLock then. – user1880779 Jan 24 '13 at 00:37
  • @user1880779: "This will be up to the user to choose his own schedule." -- which means you are already storing that schedule in a database or other form of file. After all, the user might need to edit the schedule. "If that is the case what would be the simplest way to do it?" -- not knowing your background, "simplest" is a difficult criterion. Personally, I would lean towards a database for this. – CommonsWare Jan 24 '13 at 00:40
  • So database it is. I appreciate the advice. Do you know is there any example for alarms working with a database? (don't have much experience with this). Thanks. – user1880779 Jan 24 '13 at 00:49
  • @user1880779: Possibly, though I don't have one handy. Mostly, you will tend to find separate samples on topics like these, ones for `AlarmManager` separate from ones for working with a database. – CommonsWare Jan 24 '13 at 00:55
  • Yeah that's the problem, I know basic stuff how to work with databases but I don't know what would be the most effecting way of storing those alarms and retrieving them later on boot. I just need a little push and I'll be fine. – user1880779 Jan 24 '13 at 02:38