[英]Why TimerTask is running twice?
I want to display a notification at a specific time for my android application.我想在特定时间为我的 android 应用程序显示通知。 For example: at 3 pm print the notification message.
例如:在下午 3 点打印通知消息。 I use this code:
我使用这个代码:
private void checkNotificationStatus() {
timer.schedule(new TimerTask() {
@Override
public void run() {
addNotificationReminder();
}
}, new Date(appointment.getTimeInMillis()));
}
public void addNotificationReminder() {
System.out.println("i'm here");
}
So, the timer works correctly but the message, "I'm here" is displayed twice instead of just once.因此,计时器工作正常,但消息“我在这里”显示两次而不是一次。 If I put a
long
instead of a date
as the second parameter in TimerTask
(for example 2000), the message is printed just once.如果我将一个
long
而不是date
作为TimerTask
的第二个参数(例如 2000),则消息只打印一次。 Nonetheless, if I put: appointment.getTimeInMillis() - System.currentTimeMillis()
the message is printed twice again.尽管如此,如果我输入:
appointment.getTimeInMillis() - System.currentTimeMillis()
消息将再次打印两次。
Any suggestion?有什么建议吗?
The checkNotificationStatus() is probably called two times. checkNotificationStatus() 可能被调用两次。 Maybe put a println() before timer.shedule() to check, whether the method is called multiple times or whether it really is the timers fault.
也许在 timer.shedule() 之前放一个 println() 来检查该方法是否被多次调用或是否真的是计时器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.