How to set an alarm to repeat

I am currently working on an application to set monthly reminders. I cannot provide the correct repetition interval for my alarm. Pls provide information about the same. this is my code, but it will not trigger an alarm during February or months having 30 days. Also pls provide the code for setting the alaram annual recurrence.

repeatTime=(AlarmManager.INTERVAL_DAY*31);
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi);

      

Thanks, Sharath

+3


source to share


3 answers


this is how you calculate the interval between today, a month after that, use this logic on reset alarm every time it fires. ie set the alarm to the point you want to start, put some pending intent, after the alarm triggers use below code to get the next start time, and set the alarm again to start at that time.



private long getDuration(){
    // get todays date
    Calendar cal = Calendar.getInstance();
    // get current month
    int currentMonth = cal.get(Calendar.MONTH);

    // move month ahead
    currentMonth++;
    // check if has not exceeded threshold of december

    if(currentMonth > Calendar.DECEMBER){
        // alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
        currentMonth = Calendar.JANUARY;
        // Move year ahead as well
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
    }

    // reset calendar to next month
    cal.set(Calendar.MONTH, currentMonth);
    // get the maximum possible days in this month
    int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    // set the calendar to maximum day (e.g in case of fEB 28th, or leap 29th)
    cal.set(Calendar.DAY_OF_MONTH, maximumDay);
    long thenTime = cal.getTimeInMillis(); // this is time one month ahead



    return (thenTime); // this is what you set as trigger point time i.e one month after

}

      

+5


source


for annual recurrence of alarm



GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
    if(cal.isLeapYear(year)){
      alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 366, alarmIntent);
    }else{
          alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 365, alarmIntent);
    }

      

0


source


Thanks Techfist .. ex: dateValue = "30/01/2017 11:02" ... enter id and date from database ...

private void getMonthlyDuration(Context context,int id,String dateValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("ModernDiary", Context.MODE_PRIVATE);
    String dateOfMonth = sharedPreferences.getString("day"+id,DateFormat.format("dd", Calendar.getInstance()).toString());
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
    try {
        Date dateMain = simpleDateFormat.parse(dateValue);
        calendar.setTime(dateMain);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Boolean isSetDate = false;
     if (sharedPreferences.getInt("monthInc"+id,-1) != -1) {
         calendar.set(Calendar.MONTH,sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)));
         calendar.set(Calendar.YEAR,sharedPreferences.getInt("yearInc"+id,calendar.get(Calendar.YEAR)));
         if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
             calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
             Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+" max");
         } else {
             calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
             Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+"min");
         }
         if (sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)) < calendar.get(Calendar.MONTH)){
             calendar.add(Calendar.MONTH, -1);
             isSetDate = true;
             Log.i("timeMonth","Increment "+calendar.getTime());
         } else {
             isSetDate = false;
             Log.i("timeMonth","No Change");
         }
     }
    calendar.add(Calendar.MONTH, 1);
     if (isSetDate){
         if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
             calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
         } else {
             calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
         }
     }
    Log.i("timeAf",calendar.getTime()+"");

    sharedPreferences.edit().putInt("monthInc"+id, calendar.get(Calendar.MONTH)).apply();
    sharedPreferences.edit().putInt("yearInc"+id, calendar.get(Calendar.YEAR)).apply();
    Intent notificationIntent = new Intent(context,AlarmBroadcastReceiver.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("alarm", id);
    notificationIntent.putExtra("bundle", bundle);
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    broadcast = PendingIntent.getBroadcast(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
}

      

Alarm time:   Tue Feb 28 11:02:00 GMT+05:30 2017 Thu Mar 30 11:02:00 GMT+05:30 2017 Sun Apr 30 11:02:00 GMT+05:30 2017 Tue May 30 11:02:00 GMT+05:30 2017 Fri Jun 30 11:02:00 GMT+05:30 2017 Sun Jul 30 11:02:00 GMT+05:30 2017 Wed Aug 30 11:02:00 GMT+05:30 2017 Sat Sep 30 11:02:00 GMT+05:30 2017 Mon Oct 30 11:02:00 GMT+05:30 2017 Thu Nov 30 11:02:00 GMT+05:30 2017 Sat Dec 30 11:02:00 GMT+05:30 2017 Tue Jan 30 11:02:00 GMT+05:30 2018 Wed Feb 28 11:02:00 GMT+05:30 2018 ...

0


source







All Articles