How to implement yearly and monthly repeating alarms?

前端 未结 3 1008
遥遥无期
遥遥无期 2020-12-09 00:21

I want to set monthly and yearly alarm in my app.I did this for weekly. AlarmManager.INTERVAL_DAY helped me for that.But i could not find good way to implement monthly and y

3条回答
  •  旧时难觅i
    2020-12-09 01:01

    I solved the issue. In my app,multipal alarms were set with different repeating time intervals.So in my broadcast receiver for alarm,i re-scheduled the alarm based on for what time it was scheduled to be repeated.I used calendar object to add month and year accordinly and again set it for next alarm.i used code like this(for scheduling it for 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);
    

提交回复
热议问题