I started programming now on Android, and I need to update a TextView after receiving a string from a Socket connection. I get the string correctly because apk closes giving an exception:
10-01 11: 05: 57.470: E / AndroidRuntime (753): FATAL EXCEPTION: Thread-94
I saw that this exception is caused by updating the ui by a thread outside the main thread, in that the serial solution uses handler to update the ui, but I did not get an example of how to do this and I would like to help implement this in my code:
Searching the web, I found a simple way using a separate thread to update the ui, in the following code I show what I did:
TextText class (a thread that has the function of changing text in TextView)
package com.example.palioteste;
public class AtualizarTexto implements Runnable {
private String text;
public AtualizarTexto(final String text) {
this.text = text;
}
@Override
public void run() {
MainActivity.textorecebido.setText(text);
}
}
MainActivity
package com.example.palioteste;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
private TextView textView1;
static TextView textorecebido;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView1 = (TextView) findViewById(R.id.textView1);
textorecebido = (TextView) findViewById(R.id.textorecebido);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
textView1.setText("Botao Cliquado");
abrirSocket rodar = new abrirSocket();
rodar.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thread that makes the socket connection
package com.example.palioteste;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Arrays;
public class abrirSocket extends AsyncTask {
private static final String hostname = "192.168.43.127";
private static final int portaServidor = 9837;
int valorconvertido;
@Override
protected String doInBackground(String... params) {
try {
Socket socket = new Socket(hostname, portaServidor);
//dados enviados para o servidor
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("2");
bw.newLine();
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//String retorno = "Dados Recebidos " + br.readLine(); //retornar ok
Thread t = new Thread(new AtualizarTexto(br.readLine()));
t.start();
// socket.close();
}
catch(IOException e) {
return e.getMessage();
}
return null;
}
}
This implementation did not work and I want to try to use a handler, but the examples I could not adapt to my code and I ask the help of the members for this.