简体   繁体   English

每月设置警报Android

[英]Setting alarm on monthly basis android

I am developing an android app for setting alarm on daily, weekly, monthly basis. 我正在开发一个用于每天,每周,每月设置警报的android应用。 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. Alarmtimefor30具有给定的日期(以毫秒为单位)。

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

I am giving intervalMillis as 30*1440*60000 which results to 2592000000 ie 30 days in milliseconds. 我将intervalMillis设置为30 * 1440 * 60000,结果为2592000000,即30天(以毫秒为单位)。 When I try to print 30*1440*60000 it results to 1702967296. I am not sure what could be the problem. 当我尝试打印30 * 1440 * 60000时,结果为1702967296。我不确定这可能是问题所在。

Is there another way to set monthly alarm (to trigger on specific date and time every month)? 还有另一种设置每月警报的方法(在每月的特定日期和时间触发)吗?

Please Help!Thanks! 请帮忙!谢谢!

By default, an integer literal in Java will be of type int , which is a 32-bit number. 默认情况下,Java中的整数文字将是int类型,它是一个32位数字。 When you multiply an int by an int the result is also an int , so your result is being truncated. 当你乘的intint的结果也是int ,所以你的结果被截断。 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. 显然, setRepeating的参数很long ,但这并不意味着编译器会为您解决此问题-您的乘法仍将被截断。

The solution is to explicitly force the literals to be of type long , which is a 64-bit number: 解决方案是显式强制这些文字为long类型,这是一个64位数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM