Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

android - Updating TextView every N seconds?

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

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?

Right now I have:


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;
 }
}

I guess what I'm REALLY asking is how do I make checkTime() repeat it-self endlessly until onPause() is called?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rather than fuss with a background thread and then runOnUiThread(), use postDelayed(), available on any View, to schedule a Runnable. That Runnable can update your TextView and then schedule itself for the next pass. Using a background thread for the purposes of watching time tick by is a waste.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...