0

User can create different alarms. So it's up to user when to keep alarm and he can keep multiple alarms and I maintain all the scheduled alarms in a database and show to the user for further reference. Below is my code.

if("CREATEONCE".equals(strparam1))
    {
         am.set(AlarmManager.RTC_WAKEUP, l2, pi);// l2 is time in millis
    }else if("CREATEREPEAT".equals(strparam1))
        {

    am.setRepeating(AlarmManager.RTC_WAKEUP, l2, 86400000 , pi); //l2 is time in millis
    }

So this is the code which sets the alarms. User can set multiple alarms. For example he keeps an alarm for 7.00 am for once, 8.00 am for once and 9.00 am dialy. So, for once alarms the code goes to if block and for repeat daily, the code goes to else if block in the code.

If the above 3 alarms are set by user at 6.00 am. If he reboots his device immediately after setting the alarms, the entire alarms don't trigger.

So I have read many posts regarding this like post1, post2. They all just gave to use broadcast receiver to know that device is rebooted. After the broadcast receiver receives a hint that device is rebooted, do I need to repeat above code again by getting the info from sqlite database to make all the alarms work? If so, can someone help me the way to do that from the broadcast receiver? Code snippets are appreciated

Suppose if the user sets 50 alarms, wouldn't it be a long process to get the info of all the 50 alarms and set them again?

Community
  • 1
  • 1
rick
  • 4,665
  • 10
  • 27
  • 44

1 Answers1

3

I don't know how you are storing your alarms. But I suggest it would suffice to set up a system level alarm for the earliest coming alarm. Then once that is triggered, set up the alarm again for the next soonest triggering alarm.

I suggest putting your AlarmSetting call in a service and then call it from a broadcast receiver.

public class AlarmResetReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
       //your code to set up alarms
    }
}

There are also other conditions upon which you want to set your alarms up again in your manifest

<receiver android:name=".receivers.AlarmResetReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
            <action android:name="android.intent.action.LOCALE_CHANGED" />
        </intent-filter>
    </receiver>
Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • I am setting all alarms with the above code whenever the user clicks on keep reminder for each alarm. How to keep track for the earliest alarm. Suppose if the earliest alarm doesn't trigger because the user keeps his device in off mode, then the next alarm wouldn't be triggered, right? – rick May 06 '13 at 10:45
  • That's correct, from the Broadcast receiver, go ahead and set up all of your alarms again as shown in the code I pasted. – Pork 'n' Bunny May 06 '13 at 10:46
  • If that's the case, when you are setting up the new alarms, the earliest alarm would actually be in the past. You can handle that how you see fit. – Pork 'n' Bunny May 06 '13 at 10:48
  • Okay. Thanks and can I set the alarms directly starting service in the broadcast receiver, because the second post I linked in the question suggests not to use service I guess. Can you suggest me regarding the same? – rick May 06 '13 at 10:53
  • You don't have to start up a service to set the alarms, you can do it from within the broadcast receiver. It would be common though that broadcast receivers kick off services because I imagine that many might already use a service to set the alarms because alarm setting is also done from UI. Either that or a static method that takes a context, which is what I personally do. – Pork 'n' Bunny May 06 '13 at 10:57
  • +1 and Sorry, couldn't get you. Suppose I have a method which is setAllAlarms() in which I get all alarms data from the database and set all alarms. You mean I can define and call this method in the broadcast receiver itself? Or from service? If so can you show some snippet on how you would prefer to call this method? – rick May 06 '13 at 11:12
  • you would call you "setAllAlarms()" method from the Receiver, where I have the comment "//your code to set up alarms" – Pork 'n' Bunny May 06 '13 at 11:17
  • i also faced same problem i had different alarms in different fragment i put them for 2 minutes which invoke after 2 minutes to check if any sms receive from specific number or not it work good when phone is on but when user reboot device alarm is not call its broadcast receiver. my alarms are created when user click on button which placed on different fragments kindly help me. – Madhav_nimavat Dec 17 '16 at 11:39