当前秒数等于 00 时如何启动方法
How start a method in when the current seconds equal 00
我正在 Android Studio 中的服务 class 中研究方法。
我使用 Timer 每隔一分钟重新执行一次该方法..但秒对我来说很重要..
当 onStartCommand
在 午餐时,例如 13:40:22,我们将执行 forRoom1()
方法并等待一分钟,以便它会在 13:41:22..
我希望 forRoom1()
方法在 13:40:00 开始第一次执行,因此等待一分钟后它将在 13:41:00..
重新执行
这是我的 onStartCommand 代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
forRoom1();
}
}, 0, 60 * 1000);
return START_STICKY;
}
无论现在是什么时间,最重要的是开始是00秒第一次执行
你能帮我解决这个问题吗?
第一步我们必须知道当前时间的第二个,然后执行偏移:
(60 - 实际秒数)
为此,我们使用这一行:
int second = Calendar.getInstance (). get (Calendar.SECOND);
这里我们有当前时间的秒数,我们只需要知道到达下一个时间还剩多少秒(毫秒)00s
int delay = (60 - second) * 1000;
现在我们都必须开始我们的任务,我们将使用这个方法:
public void scheduleAtFixedRate (java.util.TimerTask task,
long delay,
long period)
参数:
task – task to be scheduled.
delay – delay in milliseconds before task is to be executed.
period – time in milliseconds between successive task executions.
这是完整代码:
Timer timer = new Timer();
int second = Calendar.getInstance().get(Calendar.SECOND); // get the actual second
System.out.println(second); // check the current second on this time
int delay = (60 - second) * 1000; // this is the first delay to start the task
int period = 60000; // the period on millisSecond
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// start you task here
// you can check the time by your self with the line below
Log.e(TAG, "run: " + Calendar.getInstance().get(Calendar.MINUTE) + ":" + Calendar.getInstance().get(Calendar.SECOND));
}
}, delay, period);
注意:您可以使用timer.schedule()
方法查看此answer以获取更多信息
我正在 Android Studio 中的服务 class 中研究方法。
我使用 Timer 每隔一分钟重新执行一次该方法..但秒对我来说很重要..
当 onStartCommand
在 午餐时,例如 13:40:22,我们将执行 forRoom1()
方法并等待一分钟,以便它会在 13:41:22..
我希望 forRoom1()
方法在 13:40:00 开始第一次执行,因此等待一分钟后它将在 13:41:00..
这是我的 onStartCommand 代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
forRoom1();
}
}, 0, 60 * 1000);
return START_STICKY;
}
无论现在是什么时间,最重要的是开始是00秒第一次执行 你能帮我解决这个问题吗?
第一步我们必须知道当前时间的第二个,然后执行偏移:
(60 - 实际秒数)
为此,我们使用这一行:
int second = Calendar.getInstance (). get (Calendar.SECOND);
这里我们有当前时间的秒数,我们只需要知道到达下一个时间还剩多少秒(毫秒)00s
int delay = (60 - second) * 1000;
现在我们都必须开始我们的任务,我们将使用这个方法:
public void scheduleAtFixedRate (java.util.TimerTask task,
long delay,
long period)
参数:
task – task to be scheduled.
delay – delay in milliseconds before task is to be executed.
period – time in milliseconds between successive task executions.
这是完整代码:
Timer timer = new Timer();
int second = Calendar.getInstance().get(Calendar.SECOND); // get the actual second
System.out.println(second); // check the current second on this time
int delay = (60 - second) * 1000; // this is the first delay to start the task
int period = 60000; // the period on millisSecond
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// start you task here
// you can check the time by your self with the line below
Log.e(TAG, "run: " + Calendar.getInstance().get(Calendar.MINUTE) + ":" + Calendar.getInstance().get(Calendar.SECOND));
}
}, delay, period);
注意:您可以使用timer.schedule()
方法查看此answer以获取更多信息