在写这篇文章之前,我查看了其他帖子,但没有找到任何解决方案,我将不胜感激。
在应用程序启动器类的 onCreate 方法中,创建一个线程 TCPServer 并显示一个包含信息的表。问题是此信息会有所不同,并且当线程检测到 TCPServer 已发送新的消息控件时,必须更新此信息。
然后,如果我表达得好,我就会显示代码。
//launcher class
public class profesor extends Activity implements OnClickListener {
static TableLayout alumnsTable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
Thread serverThread = new Thread(null, new TCPServer(), "BackgroundService");
serverThread.start();
.
.
.
//information is added to the table
alumnsTable = (TableLayout)findViewById(R.id.alumnsTable);
List<Alumnos> listaAlumnos = Alumnos.ReadList();
for(Alumnos al:listaAlumnos){
alumnsTable.addView(filaAlumno(al));
}
}
//Eliminates the above information and reloads the table with the new information
public void actualiza(){
alumnsTable.removeAllViews();
setContentView(R.layout.main);
alumnsTable = (TableLayout)findViewById(R.id.alumnsTable);
List<Alumnos> listaAlumnos = Alumnos.ReadList();
for(Alumnos al:listaAlumnos){
alumnsTable.addView(filaAlumno(al));
}
}
}
//TCPServer class
public class TCPServer implements Runnable {
private static Handler handler = new Handler();
public void run() {
ServerSocket serverSocket;
String file;
int filesizeMb = 4;
int filesize = filesizeMb * (1024 * 1024); // filesize temporary hardcoded
int bytesRead;
int current = 0;
try {
serverSocket = new ServerSocket(profesor.COMUNICATION_PORT);
Log.d(profesor.LOG_TAG,"S: Servidor Iniciado.");
while (true) {
final Socket sock = serverSocket.accept();
String ClientAddr=sock.getInetAddress().toString();
ClientAddr = ClientAddr.substring(ClientAddr.indexOf('/') + 1, ClientAddr.length());
final String contacto=DeviceList.getDeviceName(ClientAddr);
Log.d(profesor.LOG_TAG,"S: Conectado con: " + ClientAddr);
// Conection type
DataInputStream dis = new DataInputStream(sock.getInputStream());
final String connectionType =dis.readUTF();
Log.d(profesor.LOG_TAG,"S: Tipo de Conexion " + connectionType);
.
.
.
.
// RECEIVING CONTROL MESSAGE
if (connectionType.contains("CONTROL")) {
String ControlText =dis.readUTF();
String[] Control = ControlText.split(":");
Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1]));
Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1]));
/****************************************************/
//Here is where I need to call the update method of the
//class teacher to delete the old data and fill the table
//again.
/****************************************************/
}
dis.close();
sock.close();
}
} catch (IOException e) {
Log.d(profesor.LOG_TAG, "IOException"+e);
e.printStackTrace();
}
}
}
谁能给我一些想法?,我已经看到了示例处理程序,但没有在类声明和另一个调用中。
我希望你明白我的意思,我的英语不是很好。
感谢您的帮助
您可以尝试使用处理程序或活动引用创建 TCPServer,然后在要更新 UI 时使用它:
refHandler.post(new Runnable(){
public void run() {
//Update
}
});
refActivity.runOnUiThread(new Runnable() {
public void run() {
//Update
}
});
使用 runOnUiThread
从 Thread 更新 UI:
// RECEIVING CONTROL MESSAGE
if (connectionType.contains("CONTROL")) {
String ControlText =dis.readUTF();
String[] Control = ControlText.split(":");
Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1]));
Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1]));
/****************************************************/
//Here is where I need to call the update method of the
//class teacher to delete the old data and fill the table
//again.
/****************************************************/
profesor.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// refresh ui here
}
});
}