Karan sharma
Karan sharma

Reputation: 1521

Set repeat alarm for every year in android

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

Upvotes: 2

Views: 2785

Answers (3)

Deval
Deval

Reputation: 255

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

Upvotes: 1

Ravinder Singh
Ravinder Singh

Reputation: 38

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

Upvotes: 2

Tung Duong
Tung Duong

Reputation: 1166

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

Upvotes: 0

Related Questions