TextView update every N seconds?

I have been very embarrassed about this recently and cannot find the answer anywhere.

When programming for android, I want to update the text box every 10 seconds, but how can I do this? I saw some examples using "Run ()" and "Update ()", but this does not help when I try, any ideas?

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 assume that I REALLY ask how can I make checkTime () repeat it endlessly until onPause () is called?

+3
source share
5 answers

How about using a timer?

private Timer timer = new Timer();
private TimerTask timerTask;
timerTask = new TimerTask() {
 @Override
 public void run() {
    //refresh your textview
 }
};
timer.schedule(timerTask, 0, 10000);

Cancel it through timer.cancel (). In your run () method, you can use runOnUiThread ();

UPDATE:

, 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");
    }
}
+10

, , runOnUiThread(), postDelayed(), View, a Runnable. Runnable TextView, . - .

+15

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);
+8

If it helps someone, this is sample code using 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);


    }
};
+5
source

Use stream. See Painless Thread .

0
source

Source: https://habr.com/ru/post/1787336/


All Articles