How to make an Android app that does something every X seconds

Hi, I want to make an application that calls a function or something else every 1 second. I have this code that does not work, can you say what is wrong?

public class App5_Thread extends Activity implements Runnable { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Thread thread = new Thread(this); thread.start(); } @Override public void run() { TextView tv1 = (TextView) findViewById(R.id.tv); showTime(tv1); try { Thread.sleep(1000); }catch (Exception e) { tv1.setText(e.toString()); } } public void showTime(TextView tv1 ){ String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis()); } 

}

+4
source share
4 answers

I changed the code following

There are 3 ways to work all (showing Log output).

But TextView is not updated. Why?

 package cz.cvut.fel.android; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class App5_RepeatedAction extends Activity { static TextView tv1; static class MyThread implements Runnable { public void run() { boolean end = false; while (!end) { String txt = "Vlakno id:" + Thread.currentThread().getId()+" THREAD"; Log.v("MyActivity", txt); //tv1.setText(txt); showTime(tv1); try { Thread.sleep(10000); } catch (InterruptedException ex) { System.err.println(ex.toString()); } } } } static void showTime(TextView tv1 ){ String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis()); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv1 = (TextView) findViewById(R.id.tv); //----------ScheduledExecutorService ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(new Runnable() { @Override public void run() { String txt = "ScheduledExecutorService"; Log.v("MyActivity", txt); //tv1.setText(txt); showTime(tv1); } }, 0, 5, TimeUnit.SECONDS); //----------TIMER Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { String txt = "Timer"; Log.v("MyActivity", txt); //tv1.setText(txt); showTime(tv1); } }, 0, 1000); //-----------THREAD try { boolean quit = false; Thread t = new Thread(new MyThread()); //t.setDaemon(true); t.start(); } catch (Exception e) { System.err.println(e.toString()); } } } 
0
source

You do not have a loop function in the run () function, try changing it as follows:

  @Override public void run() { TextView tv1 = (TextView) findViewById(R.id.tv); while(true){ showTime(tv1); try { Thread.sleep(1000); }catch (Exception e) { tv1.setText(e.toString()); } } } 

Be careful, this will go on forever. You should also use a handler to make changes to the user interface from another thread, this can be done using the following example:

 Handler mHandler = new Handler(); @Override public void run() { final TextView tv1 = (TextView) findViewById(R.id.tv); while(true){ try { mHandler.post(new Runnable(){ @Override public void run() { showTime(tv1); } ); Thread.sleep(1000); }catch (Exception e) { //tv1.setText(e.toString()); } } } 
+6
source

As I said, you need to change your textView in the user interface thread (the thread that created the component).

To do this, use a handler, for example: (Do not loop in your thread, just send a message to the handler)

 private TextView tv1; Handler tick_Handler = new Handler(); MyThread tick_thread = new MyThread(); private class MyThread implements Runnable { public void run() { String txt = "Vlakno id:" + Thread.currentThread().getId()+" THREAD"; Log.v("MyActivity", txt); //tv1.setText(txt); showTime(tv1); tick_Handler.postDelayed(tick_thread, 1000); } } String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); private void showTime(TextView tv ){ Calendar cal = Calendar.getInstance(); tv.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis()); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv1 = (TextView) findViewById(R.id.tv); tick_Handler.post(tick_thread); } 

By the way, if you want to have an accurate timer, you should indicate every 300 ms. You may see some strange seconds if you execute your "showtime" method every second.

+3
source

You seem to have two problems.

1 - If there is no loop in your thread, the showtime method will be called only once.

2 - Changing the user interface component from a thread other than the main thread will not be performed.

You should find what you want here: http://developer.android.com/resources/articles/timed-ui-updates.html

+1
source

All Articles