Frage

I have created a reminder app which gives notification "n" days before a fixed date. I came across an interesting bug while trying out the program. I have made the code such that if the "nth day" is before the current time the alarm does not set.

However there is a bug somewhere in the code. If i set an alarm for today (dec 6), only n=0 should set the alarm, however the alarm also gets set if i enter in number 25 to 50. if i set n to 25 alarm is set to 31st dec and if n is set to 50 alarm is set to today i.e. 6th december.

The code i am using to subtract time is:

Long i = c.getTimeInMillis() - n*24*60*60*1000;
c1.setTimeInMillis(i);

also

  • value of i for n = 0 is 1386315000678
  • value of i for n = 50 is 1386289967974

CurrentTimeInMillis is 1386337498776

Is there something wrong with the code? Or is this something to do with how time converts to milliseconds in android?

War es hilfreich?

Lösung

I used :

    c.add(Calendar.DAY_OF_YEAR, 0-n);
c1.setTimeInMillis(c.getTimeInMillis());
    c.add(Calendar.DAY_OF_YEAR, n);

this works without giving the funny unexplainable bug

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top