i tried to make the schedule()
method wait till a specific hour and minute comes and start to execute the task but it didnt work because the method schedule()
doesnt take hours and minutes as an argument, what is the replacment for this method? i'm i supposed make schedule for hours then when the hour comes the program check the minutes? then if both are correct it will start executing or what?
我试图让schedule()方法等到一个特定的小时和分钟到来并开始执行任务但它没有工作,因为方法schedule()不需要花费数小时和分钟作为参数,这个方法的替换是什么?我应该按小时制定时间表,然后当小时计时检查会议记录?然后,如果两者都正确,它将开始执行或什么?
P.S for making things easier to understand, how to make something similier to this :
P.S使事情更容易理解,如何使事情更相似:
schedule(new TimerClass(), desired hours, desired minutes)?
schedule(新的TimerClass(),所需的小时数,所需的分钟数)?
2 个解决方案
#1
0
If you want to schedule for a particular clock time (rather than a delay from current time), here are some options:
如果您想安排特定的时钟时间(而不是从当前时间延迟),这里有一些选项:
- Use Quartz, if you're doing this enough to justify the additional library. Quartz makes it easy to specify clock times, and supports scheduling recurring events.
- Otherwise, at the time you're scheduling, compute the difference between the current time and the desired time. (Use the Calendar class to create clock times, convert to Dates, and getMillis().) Feed the difference to Timer -- or ScheduledThreadPoolExecutor.
使用Quartz,如果你这样做足以证明其他库的合理性。 Quartz可以轻松指定时钟时间,并支持调度重复事件。
否则,在您安排时,计算当前时间与所需时间之间的差异。 (使用Calendar类创建时钟时间,转换为Dates和getMillis()。)将差异输入Timer - 或ScheduledThreadPoolExecutor。
#2
0
You don't need to such a hardcoding.Testing hour then minutes.Just schedule the task like below. The task given below will execute after one hour and will be repeated in each 30000ms.If you need execution only once then cancel the time after first execution like using t.cancel() and also convert Hour+minute to ms to put the scheduled time
你不需要这样的硬编码。测试时间然后是几分钟。只需安排下面的任务。下面给出的任务将在一小时后执行,并将在每个30000ms重复执行。如果您只需要执行一次,则在第一次执行后取消时间,如使用t.cancel(),并将Hour + minute转换为ms以放置预定时间
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//do your task here
}
});
}};
t.schedule(scanTask, 1*60*60*1000, 30000);
#1
0
If you want to schedule for a particular clock time (rather than a delay from current time), here are some options:
如果您想安排特定的时钟时间(而不是从当前时间延迟),这里有一些选项:
- Use Quartz, if you're doing this enough to justify the additional library. Quartz makes it easy to specify clock times, and supports scheduling recurring events.
- Otherwise, at the time you're scheduling, compute the difference between the current time and the desired time. (Use the Calendar class to create clock times, convert to Dates, and getMillis().) Feed the difference to Timer -- or ScheduledThreadPoolExecutor.
使用Quartz,如果你这样做足以证明其他库的合理性。 Quartz可以轻松指定时钟时间,并支持调度重复事件。
否则,在您安排时,计算当前时间与所需时间之间的差异。 (使用Calendar类创建时钟时间,转换为Dates和getMillis()。)将差异输入Timer - 或ScheduledThreadPoolExecutor。
#2
0
You don't need to such a hardcoding.Testing hour then minutes.Just schedule the task like below. The task given below will execute after one hour and will be repeated in each 30000ms.If you need execution only once then cancel the time after first execution like using t.cancel() and also convert Hour+minute to ms to put the scheduled time
你不需要这样的硬编码。测试时间然后是几分钟。只需安排下面的任务。下面给出的任务将在一小时后执行,并将在每个30000ms重复执行。如果您只需要执行一次,则在第一次执行后取消时间,如使用t.cancel(),并将Hour + minute转换为ms以放置预定时间
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//do your task here
}
});
}};
t.schedule(scanTask, 1*60*60*1000, 30000);