user3296952
user3296952

Reputation: 3

I want to set an alarm repeatedly, like yearly, monthly, weekly in android, how can i do it?

I'm able to set a repeating alarm on the same day, but when I try to do the same for yearly or monthly (like on the same day of every month alarm should set), it's not working.

Following is the code i have tried for the same day

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, present_calender.getTimeInMillis()+intveral,intveral, pendingIntent);

I tried to search the same thing in stack overflow, but I couldn't find the exact solution so that's the reason I'm posting this question again.

Can anyone suggest me the solution (if possible, please, share some sample code)

Thanks in advance :-)

Upvotes: 0

Views: 1264

Answers (1)

youssefhassan
youssefhassan

Reputation: 1065

Try this code

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this.getApplicationContext(),
                your_destined_class.class);
intent.setAction(your_action_name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar timeOff = Calendar.getInstance();
        timeOff.add(Calendar.MILLISECOND,
        **your_duration_in_ms**);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        timeOff.getTimeInMillis(),
        **your_duration_in_ms**,
        pendingIntent);

and set your_duration_in_ms to daily(24*60*60*1000) or monthly(30*24*60*60*1000) and so on

Upvotes: 1

Related Questions