Android定时器:实现定时的几种方法

时间:2021-12-13 03:22:06

当需要更新UI时都需要用到Handler类,否则可以直接处理。

1、用Timer和TimerTask实现

Timer里面有几个比较重要的方法:

void cancel()//取消timer还有任务
Cancels the Timer and all scheduled tasks

void schedule(TimerTask task, long delay, long period)//循环调用可以用此方法,一次调用则是调用另外一个
Schedule a task for repeated fixed-delay execution after a specific delay.//没有最后一个参数的重载方法

void schedule(TimerTask task, Date when, long period)//同上
Schedule a task for repeated fixed-delay execution after a specific time has been reached.

TimerTask最重要的就是重写run方法

实例:

public class MainActivity extends ActionBarActivity {
TextView textView;
Button button;
Handler handler = new Handler() { //处理消息的handler
public void handleMessage(android.os.Message msg) {
textView.setText(msg.obj.toString() + " have excute the task for "
+ msg.what + " seconds");
}
};

Timer timer = new Timer(); //实例化一个Timer
TimerTask task = new TimerTask() { //实例化一个TimerTask重写run方法
<span style="white-space:pre"></span>private int i = 1;
@Override
public void run() {
// TODO Auto-generated method stub
Message message = Message.obtain();
message.obj = "leelit";
message.what = i++;
handler.sendMessage(message);
if (i > 10)
timer.cancel();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
timer.schedule(task, 1000, 1000); //让task以一秒为周期执行
}
});
}
}

2、用线程的sleep

public class MainActivity extends ActionBarActivity {
TextView textView;
Button button;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
textView.setText(msg.obj.toString() + " has excuted the task for "
+ msg.what + " seconds");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new Thread(new TimeTaskThread()).start(); //点击让线程开始
}
});

}
private class TimeTaskThread implements Runnable {
private int i = 1;
private boolean flag = true;
@Override
public void run() {
// TODO Auto-generated method stub
while (flag) {
Message message = Message.obtain();
message.obj = "leelit";
message.what = i++;
handler.sendMessage(message);
if (i > 10) //终结线程的标志
flag = false;
try { //发送完消息,就让他休眠
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

3、用Handler类

final booleanpostDelayed(Runnable r, long delayMillis) //特定时间后执行Runnable对象的run方法,要实现循环则在run方法里面再次执行这个方法
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.

final voidremoveCallbacks(Runnable r) //将这个Runnable对象移除出消息队列
Remove any pending posts of Runnable r that are in the message queue.

public class MainActivity extends ActionBarActivity {
TextView textView;
Button button;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
textView.setText(msg.obj.toString() + " have excute the task for "
+ msg.what + " seconds");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
handler.postDelayed(new TimeTaskThread(), 1000);
}
});
}
private class TimeTaskThread implements Runnable {
private int i = 1;

@Override
public void run() {
// TODO Auto-generated method stub
Message message = Message.obtain();
message.obj = "leelit";
message.what = i++;
handler.sendMessage(message);
handler.postDelayed(this, 1000); //实现循环执行任务
if (i > 10)
handler.removeCallbacks(this);
}
}

}

小结:用Timer的 void  schedule(TimerTask task, long delay, long period)方法,用Handler的 final boolean  postDelayed(Runnable r, long delayMillis)方法可以实现定时执行任何,也可以让其循环执行。此外也可以用线程睡眠的方法。