Multiple Alarms Restart After Boot

≡放荡痞女 提交于 2019-12-23 02:35:09

问题


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

            }        

回答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.



来源:https://stackoverflow.com/questions/14490871/multiple-alarms-restart-after-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!