Alarm Notification fires instantly. Android

后端 未结 5 2011
忘掉有多难
忘掉有多难 2021-02-15 18:15

I am working on a Reminder that sends notification on fixed time to the user.

The alarm is getting off instantly ...

I tried most of the suggestions over s

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 18:47

    The main problem appears to be with this line:

    calAlarm.set(Calendar.DAY_OF_WEEK, viewFunctions.getDayInt(strDays[j]));
    

    What you need to realise is this is just setting the Day of the week which will be displayed in output - It is not changing the underlying date to match, which I think is what you are expecting.

    Try using the following code to alter your dates to set an alarm for each day selected:

    String strSpit[] = time.split(":");
    String strDays[] = reminderList.get(i).getDays().split(","); //"days": "1,2,3,4,5,6,7"
    
    Calendar todayWithTime = Calendar.getInstance(); //setting current time is redundant
    todayWithTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strSpit[0]));
    todayWithTime.set(Calendar.MINUTE, Integer.parseInt(strSpit[1]));
    
    Calendar alarm;
    int today = todayWithTime.get(Calendar.DAY_OF_WEEK);
    int offset, target;
    
    for (int j = 0; j < strDays.length; j++) {
    
        alarm = (Calendar) todayWithTime.clone(); //now you have todays date, but correct time
        target = strDays[j];
        //saturday is biggest day of week
        offset  = (Calendar.SATURDAY - today + target) % 7; //work out how many days in the future the next occurance of this day is
        alarm.add(Calendar.DATE, offset);
    
        ... // the rest stays the same
    
    }
    

提交回复
热议问题