[英]AlarmManager fires alarm in the wrong time if timezone is different than GMT(UTC)
I know this question was asked a billion times, and I have really tried to make use of the answers but this just can't seem to work as i expect it to. 我知道这个问题被问了十亿次,我确实试图利用答案,但这似乎无法按我期望的那样工作。 I'm having trouble setting the alarms via AlarmManager - everything seems to be working fine with UTC ( or GMT -whatever you prefer), but when it comes to other timezones the alarms are not fired at the right time.
我在通过AlarmManager设置警报时遇到问题-一切似乎都可以在UTC(或GMT-无论您喜欢什么)下正常工作,但是在其他时区,警报不会在正确的时间触发。 If i set an alarm at 12:00 if my timezone is (+2) it fires up at 14:00, 2 hrs late.
如果我在时区为(+2)的12:00设置了闹钟,则会在14:00(晚2小时)触发。
Here's a snippet: 这是一个片段:
//todays date in seconds
long current = (System.currentTimeMillis()/86400000)*86400;
long time =current + 43200;
alarmManager.set(AlarmManager.RTC_WAKEUP, time*1000,
operationBroadcast);
The time on the device is 6:42 AM Here's what i've found in the alarmmanagers "dump" file 设备上的时间是6:42 AM这是我在alarmmanagers“转储”文件中找到的内容
RTC_WAKEUP #10: Alarm{420f49b8 type 0 com.pocketenergy.androidbeta}
type=0 when=+7h17m37s418ms repeatInterval=0 count=0
operation=PendingIntent{420f3b20: PendingIntentRecord{420f4808 com.pocketenergy.androidbeta broadcastIntent}}
As far as i've read on several places the time should be set in UTC whatever timezone is selected on the actual device, but i have tried a lot of different stuff with the Calendar, for example: 据我在几个地方阅读的时间,无论在实际设备上选择了哪个时区,都应该在UTC中设置时间,但是我尝试使用Calendar进行很多其他操作,例如:
//todays date in seconds
long current = (System.currentTimeMillis()/86400000)*86400;
long time =current + 43200;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time*1000);
TimeZone timeZone= TimeZone.getTimeZone("UTC"); //also tried TimeZone.getDefault();
calendar.setTimeZone(timeZone);
time = cal.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, time,
operationBroadcast);
Same outcome. 结果一样。 Ideas, anybody?
想法,有人吗? :)
:)
So, I finally got this to work. 因此,我终于使它起作用。 Here's the code :)
这是代码:)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, operationBroadcast);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.