Search code examples
javaandroidmathalarmmanager

Setting alarm on monthly basis android


I am developing an android app for setting alarm on daily, weekly, monthly basis. The first two are working fine by converting the give date and time into milliseonds. But when I am trying to do the same for monthly it doesn't work. There is totally different date format.

I am setting it as below,

Alarmtimefor30 has the given date in milliseconds.

  am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTimefor30, 30*1440*60000 , pi);

I am giving intervalMillis as 30*1440*60000 which results to 2592000000 i.e 30 days in milliseconds. When I try to print 30*1440*60000 it results to 1702967296. I am not sure what could be the problem.

Is there another way to set monthly alarm (to trigger on specific date and time every month)?

Please Help!Thanks!


Solution

  • By default, an integer literal in Java will be of type int, which is a 32-bit number. When you multiply an int by an int the result is also an int, so your result is being truncated. Obviously the argument to setRepeating is a long, but that doesn't mean the compiler will fix this for you - your multiplication will still be truncated.

    The solution is to explicitly force the literals to be of type long, which is a 64-bit number:

    am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTimefor30, 30L*1440L*60000L , pi);