TextView的更新每隔N秒?每隔、TextView

2023-09-12 06:43:14 作者:那片天何时才能再为我而蓝

我一直很困惑这个最近并不能找到答案的任何地方。

I've been very confused about this recently and can't find an answer anywhere.

当编程为Android,我想更新每10秒一个TextView,但我将如何去的?我已经看到了一些示例使用运行()和更新(),但似乎并没有帮助,当我尝试它,任何想法?

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();

更新:

我有一个livescoring的应用程序,它使用这个定时器来更新它每隔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");
    }
}
 
精彩推荐
图片推荐