每N秒更新一次TextView?

我最近对此感到非常困惑,无法在任何地方找到答案。 在为Android编程时,我想每10秒更新一次textview,但是我该怎么做呢?我看过一些样本使用“Run()”和“Update()”,但是当我尝试它时,这似乎没有任何帮助,任何想法? 现在我有:
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.slideshow);

  CONST_TIME = (int) System.currentTimeMillis();

  Resources res = getResources();        
  myString = res.getStringArray(R.array.myArray);
}

public void checkTime(View V){
 TextView text = (TextView) findViewById(R.id.fadequote);
 CUR_TIME = (int) System.currentTimeMillis();
 text.setText(""+(int) (CUR_TIME-CONST_TIME));//Debugs how much time has gone by

 if(CUR_TIME-CONST_TIME>10000){
  getNextQuote(null); //A function that gets a random quote
  CONST_TIME = CUR_TIME;
 }
}
我想我真正要问的是如何让checkTime()无休止地重复它 - 直到onPause()被调用?     
已邀请:
那么使用计时器呢?
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(); 更新: 我有一个livescoring应用程序,它使用此计时器每30秒更新一次。它看起来像这样:
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");
    }
}
    
而不是用后台线程然后
runOnUiThread()
大惊小怪,使用
postDelayed()
,可在任何
View
上,安排
Runnable
。那
Runnable
可以更新你的
TextView
然后安排自己下一次通过。使用后台线程来观察时间流逝是一种浪费。     
我同意Wired00的回答,但请遵循以下命令:
        //update current time view after every 1 seconds
        final Handler handler=new Handler();

        final Runnable updateTask=new Runnable() {
            @Override
            public void run() {
                updateCurrentTime();
                handler.postDelayed(this,1000);
            }
        };

        handler.postDelayed(updateTask,1000);
    
这可以帮助这里有人使用
postDelayed()
的示例代码 ...
private Handler mHandler = new Handler();
...
// call updateTask after 10seconds
mHandler.postDelayed(updateTask, 10000);
...
private Runnable updateTask = new Runnable () {
    public void run() {
        Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
                "updateTask run!");

                    // run any code here...         

                    // queue the task to run again in 15 seconds...
                    mHandler.postDelayed(updateTask, 15000);


    }
};
    
使用线程。请参阅无痛线程。     

要回复问题请先登录注册