How to perform annual and monthly recurring alarms?

I want to set a monthly and annual alarm in my application. I did it in a week. AlarmManager.INTERVAL_DAY helped me with this. But I could not find a good way to implement a monthly and annual repeat.

Searched so far:

http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=3503

http://groups.google.com/group/android-developers/browse_thread/thread/9f946e40307073c4?pli=1

Is there any other way to do this? Any help was appreciated.

+6
source share
3 answers

I solved the problem. In my application, multi-user alarms were set at different repeating time intervals. So in my alarm broadcast receiver, I rescheduled the alarm based on what time it was supposed to be repeated. I used the calendar object to add the month and year according to and set it again for the next alarm. The code used is (for planning it for the next month) -

PendingIntent sender = PendingIntent.getBroadcast(context,Integer.parseInt(Long.toString(id)), intent1, 0); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(calendar.getTimeInMillis()); calendar.add(Calendar.SECOND, 30); AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); calendar.add(Calendar.MONTH, 1); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
+3
source

I think you have two inherent problems with this approach:

  • AlarmManager will not accept large periods of time because the number of milliseconds will overflow the argument

  • I do not think that Alarms will survive the reboot of your phone, which will certainly happen over such a long period of time.

I advise you to keep each alarm in a safe place and use a combination of AlarmManager and onBoot receivers to check if you need to fire one of the alarms from your list that day and just move the alarm to wake you up tomorrow, if not.

 public class AlarmService extends Service { //compat to support older devices @Override public void onStart(Intent intent, int startId) { onStartCommand(intent, 0, startId); } @Override public int onStartCommand (Intent intent, int flags, int startId){ //your method to check if an alarm must be fired today checkForTodayAlarmsAndBehaveAppropriately(); //reschedule me to check again tomorrow Intent serviceIntent = new Intent(AlarmService.this,AlarmService.class); PendingIntent restartServiceIntent = PendingIntent.getService(AlarmService.this, 0, serviceIntent,0); AlarmManager alarms = (AlarmManager)getSystemService(ALARM_SERVICE); // cancel previous alarm alarms.cancel(restartServiceIntent); // schedule alarm for today + 1 day Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, 1); // schedule the alarm alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), restartServiceIntent); } } 

To start the service at boot time, use this:

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class serviceAutoLauncher extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context,AlarmService.class); context.startService(serviceIntent); } } 

Finally, add this to your manifest to schedule serviceAutoLauncher to run on every boot:

  <receiver android:name="serviceAutoLauncher"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> <category android:name="android.intent.category.HOME"></category> </intent-filter> </receiver> 
+17
source

To add Laurent to the answer. To ignore the calculation of repetition manually, I suggest taking a look at this RFC-2445 implementation . It works great on Android and can save you a lot of pain.

+3
source

Source: https://habr.com/ru/post/898140/


All Articles