-1

I try to refresh TextView in Android. I am using ExecutorService in my code. When I pushed the "Start refresh" button, after several seconds my app has been stopped by Android system. I have read several topics in stackoverflow.com, but I still cannot understand what I am doing wrong. Here is my code:

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity {
    private static ExecutorService exec;
    private static TextView textView;
    private static Calendar cal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text_field);
        cal = new GregorianCalendar();
        exec = Executors.newSingleThreadExecutor();
    }

    private static class Task implements Runnable {
        @Override
        public void run() {
            try {
                while(!Thread.currentThread().isInterrupted()) {
                    textView.setText(cal.getTime().toString());
                    TimeUnit.SECONDS.sleep(1);
                }
            }
            catch (InterruptedException e) {
                return;
            }
        }
    }

    public void finish_refresh(View view) {
        exec.shutdownNow();
    }

    public void start_refresh(View view) {
        exec.submit(new Task());

    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/start_refresh"
        android:onClick="start_refresh"/>

    <Button android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/finish_refresh"
        android:onClick="finish_refresh"/>

    <TextView android:layout_height="200dp"
        android:layout_width="200dp"
        android:layout_marginTop="10dp"
        android:id="@+id/text_field" />

</LinearLayout>
Alex
  • 751
  • 1
  • 9
  • 28

1 Answers1

1

You need to use runOnUiThread() Add following code in your onCreate() method:

Thread t = new Thread() {
    @Override
    public void run() {
        try {
             while (!isInterrupted()) {
                    Thread.sleep(1000);                  
                    runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                          // update TextView here!
                          textView.setText(cal.getTime().toString());
                      }
                 });
             }
          } catch (InterruptedException e) { } 
     }
};

t.start();
Iqbal S
  • 1,156
  • 10
  • 16
  • I have seen this solution in another question - but it doesn't work. I works only one time. It looks like the thread has suspended, or terminate. I tried it with ExecutorService, ScheduledThreadPoolExecutor, simple threads - nothing happen. It works only one time, or it doesn't work at all. Maybe I should turn on some settings in my phone (Samsung S3) ? – Alex Apr 19 '15 at 16:16
  • @lqbal Thank you for the answer, you was absolutely right. I had mistake in the ExecutorService. I separated one task into two - "class Task" and "class UITask". One of them runs another one using runOnUiThread. After it everything works! – Alex Apr 20 '15 at 07:36