Android - Call method from an Activity from a Thread
I have the following snippet:
public class SocketActivity extends Activity {
.
.
.
EditText textView;
Socket socket = new Socket(name,ip);
out = new PrintWriter(socket.getOutputStream());
out.flush();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
.
.
.
}
Now, I have other lines to allow this activity to converse with a java server. But what I would like to do, is create a thread that constantly listens for a message from the server. Currently, it only listens for a response after I send a message. How would I set the textView from the thread? Is this the best way to do this? If not, where else should I do this?
======================
So, here is a more complete description of what I want to do, the textView is too primitive for what I want. So I have a MainActivity that creates a superclassed SurfaceView which has a superclassed Renderer that contains a Cube object with buffers, so it can be drawn. I also have a SocketActivity to connect to a server
What I want to do is wait for the server to send a packet of points to the client. Then, as each point is received it is put into the buffer. Then, once it completes, it waits again for the next packet. How should I use Asynctask be used to put a point to a buffer everytime I receive one. So to get a map of what's going on:
public class MainActivity extends Activity {
OpenGLSurfaceView view;
.
开发者_如何学Python .
.
}
public class OpenGLSurfaceView extends SurfaceView {
OpenGLRenderer renderer;
.
.
.
}
public class OpenGLRenderer extends Renderer {
Cube cube;
.
.
.
}
public class Cube{
private FloatBuffer vertexBuffer;
.
.
.
}
public class SocketActivity extends Activity {
Socket socket;
BufferedReader in;
PrintWriter out;
.
.
.
}
Now where should the Asynctask be instantiated? Should I structure my program this way, or is there a better way? (For those that do not know, to put a primitive into a buffer you use buffer.put(/*primitive*/), but this is trivial.)
You cannot modify UI (in this case TextView) from non-UI thread. Try using AsyncTask. The UI can be modified ONLY in onPreExecute() and onPostExecute() methods. If you need example of how to use AsyncTask, please ask for.
There is another way to use Runnable with the method runOnUiThread() but, I recommend AsyncTask.
Example:
private class Surgery extends AsyncTask<String, Void, String> {
private String dataSurgery=null;
private File fileSurgery=null;
public Surgery (String data1, File data) //constructor
{
this.dataSurgery = data1; //this is just example here you can put anything you want
this.fileSurgery = data;
}
@Override
protected String doInBackground(String... params) {
//OPERATION WHICH IS UI-INDEPENDENT LET'S SAY SOMETHING LIKE THIS:
Socket socket = new Socket(name,ip);
out = new PrintWriter(socket.getOutputStream());
out.flush();
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); return "here you return data for the textview or anything you need";
}
@Override
protected void onPostExecute(String result) {
// HERE YOU UPDATE YOUR TEXTVIEW i.e HERE YOU UPDATE ANYTHING WHICH IS ON THE UI
}
@Override
protected void onPreExecute() {
// This is OPTIONAL, you might find you dont need to use it.
}
}
}
精彩评论