How to use setText from a service running in java?

Asked

Viewed 97 times

0

I have a service running a stopwatch that loops from one in a second, how to set the text in the open Activity? I’m using:

//tempo

         new Thread(new Runnable() {

               @Override
               public void run(){

                   int jumpTime = 30;
                   int limiteTempo = 0;
                   String id_chamado = String.valueOf(id_cham);
                   Log.i("aviso","id chamado"+id_chamado);
                  while(limiteTempo < 31){

                     try {
                         Thread.sleep(1000);
                         if(jumpTime==0){

                            Log.i("aviso","acabou tempo");
                            break;
                         }

                            Log.i("aviso",String.valueOf(jumpTime ));
                            jumpTime = jumpTime -1;
                            limiteTempo++;
                     } catch (InterruptedException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                     }

                  }

               }
               }).start();

1 answer

1


How you are running code on a thread separate, it is necessary that all interface requests are executed in the thread which is responsible for the interface since android UI is not "thread-safe". There are several options to resolve this impasse, in your case, I think it appropriate to use the runOnUiThread if you are inside an Activity:

package com.iuridiniz.androidclockexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout fl = new FrameLayout(this);
        /* qualquer objeto acessado a partir de outra innerclass tem que ser declarado como imutável (final) */
        final TextView tv = new TextView(this);
        fl.addView(tv);
        setContentView(fl);

        Thread th = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Calendar c = Calendar.getInstance();
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                tv.setText(df.format(c.getTime()));
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        th.start();
    }
}

Note that textView is declared as final, only so it can be accessed in innerclass raised in runOnUiThread(new Runnable() { ... }).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.