Setting alarm on monthly basis android

谁都会走 提交于 2019-12-10 18:13:06

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/17857920/setting-alarm-on-monthly-basis-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!