2

I am building an alarm application in android.Till now i am able to create repeat alarm for every day,week and every month.

Now my requirement is to set repeat alarm for every year. TIA

Karan sharma
  • 1,511
  • 1
  • 13
  • 37
  • can you show us the code you are using for setting it by day/week/month? shouldnt be any different for year. – letsCode Jan 18 '18 at 18:35
  • To set it repeat every year. e.g. if i set alarm for 19-1-2018,then next alarm should fire automatically on 19-1-2019,19-1-2020 and so on... – Karan sharma Jan 19 '18 at 05:02

3 Answers3

2

You can set alarm after every year, each time alarm is fired you can start a new service intent which will be set alarm for next consecutive year.

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);
  • 1
    will the alarm service be active after 1 year on phone ? Is there an actual test here that proves that AlarmManager will still fire an alarm after a very long period of time? – ralphgabb Oct 11 '18 at 02:44
1

This will help you to repeat alarm every year. Use DateUtils this will ease your work if you are working on Alarm App.

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alrarmTime, DateUtils.YEAR_IN_MILLIS, pendingIntent);
Deval
  • 245
  • 1
  • 9
0

May be this is answer https://developer.android.com/training/scheduling/alarms.html

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

I think you can set alarm for next year, when time come alarmIntent run and set alarm for year + 1 , this can make it repeat every year

// Set the alarm to start at approximately next year
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);
Tung Duong
  • 1,156
  • 7
  • 19
  • this to set repeating alarm at 14:00 for everyday i am asking to set it repeat every year. e.g. if i set alarm for 19-1-2018,then next alarm should fire automatically on 19-1-2019,19-1-2020 and so on... – Karan sharma Jan 19 '18 at 05:02
  • 1
    I think you can do like set alarm for next year, when alarmIntent call for start service, this service will set alarm again for year + 1. I will add more example in answer – Tung Duong Jan 19 '18 at 09:39