How to use setText from a service running in java?

0

I have a service that runs a timer that loops one by one, 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();
    
asked by anonymous 28.02.2015 / 20:17

1 answer

1

As you are executing code in a separate thread , all interface requests must be executed in the thread which is responsible for the interface, the android UI is not "thread-safe" . There are several options to resolve this deadlock , in your case, I think indicated use the < a href="https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)"> 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 end , so it can only be accessed in the innerclass created in runOnUiThread (new Runnable ) {...}) .

    
01.03.2015 / 12:00