每N秒更新一次TextView? [英] Updating TextView every N seconds?
问题描述
我最近对此非常困惑,无法在任何地方找到答案.
I've been very confused about this recently and can't find an answer anywhere.
在为 android 编程时,我想每 10 秒更新一次 textview,但我该怎么做呢?我已经看到一些示例使用Run()"和Update()",但是当我尝试时这似乎没有帮助,有什么想法吗?
When programming for android, I want to update a textview every 10 seconds, but how would I go about that? I've seen some samples use "Run()" and "Update()", but that doesn't seem to help when I try it, any ideas?
现在我有:
我想我真正要问的是如何让 checkTime() 无休止地重复它,直到 onPause() 被调用?
I guess what I'm REALLY asking is how do I make checkTime() repeat it-self endlessly until onPause() is called?
推荐答案
使用定时器怎么样?
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");
}
}
这篇关于每N秒更新一次TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!