Android monthly alarm setup

I am developing an Android app to set up alarms daily, weekly, monthly. The first two work fine, converting the date and time of dates into milliseasons. But when I try to do the same in a month, it will not work. There is a completely different date format.

I install it as shown below

Alarmtimefor30 has a given date in milliseconds.

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

I give the Millis interval as 30 * 1440 * 60000, which leads to 2592000000, i.e. 30 days in milliseconds. When I try to print 30 * 1440 * 60,000, the result will be 1702967296. I am not sure what might be the problem.

Is there another way to set a monthly alarm (run at a specific date and time every month)?

Please, help! Thanks!

+4
source share
1 answer

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

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

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

Source: https://habr.com/ru/post/1493427/


All Articles