更新时间:2022-12-28 17:13:12
使用定时器怎么样?
private Timer timer = new Timer();
private TimerTask timerTask;
timerTask = new TimerTask() {
@Override
public void run() {
//refresh your textview
}
};
timer.schedule(timerTask, 0, 10000);
通过 timer.cancel() 取消它.在你的 run() 方法中,你可以使用 runOnUiThread();
Cancel it via timer.cancel(). In your run() method you could use runOnUiThread();
更新:
我有一个实时评分应用程序,它使用此计时器每 30 秒更新一次.它看起来像这样:
I have a livescoring app, which uses this Timer to update it every 30 sec. It looks like this:
private Timer timer;
private TimerTask timerTask;
public void onPause(){
super.onPause();
timer.cancel();
}
public void onResume(){
super.onResume();
try {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
//Download file here and refresh
}
};
timer.schedule(timerTask, 30000, 30000);
} catch (IllegalStateException e){
android.util.Log.i("Damn", "resume error");
}
}