重复延迟任务n次
Repeat delayed task n times
我想 运行 部分代码 n 次,延迟几秒。
这是我的代码:
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e("myLog","Runnable()-->Run()");
// do a task here
}
};
Handler handler = new Handler();
// loop repeating task 6 times
for (int count = 0; count < 6; count++){
Log.e("Log","Task loop "+count);
handler.postDelayed(runnable, 20000); // run task after 20 seconds
}
问题:for 循环运行并发执行所有任务。我要运行一个接一个延迟任务
我在 post 找到了答案:-
Repeat a task with a time delay?
但它会无限次地重复作业。
我发现我的问题非常符合逻辑:-
但看起来与我无关
你可以试试这个,
int n=0;
myHandler=new Handler();
myRunnable=new Runnable() {
@Override
public void run() {
//do your task
n++;
if(n<=count)
myHandler.postDelayed(this,2000);
}
};
myRunnable.run();
// Instance variable
private int counter = 0;
private int maxCounter = 6;
createTask(){
if(counter<maxCount){
counter++;
handler.postDelayed(runnable, 20000);
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e("myLog","Runnable()-->Run()");
// do a task here
createTask();
} catch (IOException e) {
Log.e("myLog "+e.toString());
}
}
};
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
class RepeatableTask extends TimerTask{
int repeats;
Timer time;
public RepeatableTask(int repeats){
this.repeats=repeats;
}
void init(){
time = new Timer();
time.schedule(this,0,TimeUnit.MINUTES.toMillis(delayInMinutes));
}
void stop(){
time.cancel();
}
void run(){
if(repeats == 0){stop();}
new Thread->{
//task
}
repeats--;
}
}
//usage
RepeatableTask taskObject = new RepeatableTask(5);
taskObject.init();
我想 运行 部分代码 n 次,延迟几秒。
这是我的代码:
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e("myLog","Runnable()-->Run()");
// do a task here
}
};
Handler handler = new Handler();
// loop repeating task 6 times
for (int count = 0; count < 6; count++){
Log.e("Log","Task loop "+count);
handler.postDelayed(runnable, 20000); // run task after 20 seconds
}
问题:for 循环运行并发执行所有任务。我要运行一个接一个延迟任务
我在 post 找到了答案:- Repeat a task with a time delay?
但它会无限次地重复作业。
我发现我的问题非常符合逻辑:-
但看起来与我无关
你可以试试这个,
int n=0;
myHandler=new Handler();
myRunnable=new Runnable() {
@Override
public void run() {
//do your task
n++;
if(n<=count)
myHandler.postDelayed(this,2000);
}
};
myRunnable.run();
// Instance variable
private int counter = 0;
private int maxCounter = 6;
createTask(){
if(counter<maxCount){
counter++;
handler.postDelayed(runnable, 20000);
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e("myLog","Runnable()-->Run()");
// do a task here
createTask();
} catch (IOException e) {
Log.e("myLog "+e.toString());
}
}
};
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
class RepeatableTask extends TimerTask{
int repeats;
Timer time;
public RepeatableTask(int repeats){
this.repeats=repeats;
}
void init(){
time = new Timer();
time.schedule(this,0,TimeUnit.MINUTES.toMillis(delayInMinutes));
}
void stop(){
time.cancel();
}
void run(){
if(repeats == 0){stop();}
new Thread->{
//task
}
repeats--;
}
}
//usage
RepeatableTask taskObject = new RepeatableTask(5);
taskObject.init();