如何显示处理器/线程内敬酒? [英] How to display a Toast inside a Handler/thread?
本文介绍了如何显示处理器/线程内敬酒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示敬酒,一旦消息被发送到一个socket.After这个 Log.d(ClientActivity,C:发送);
I want to display a toast once the message is sent to a socket.After this "Log.d("ClientActivity", "C: Sent.");"
我是否需要创建一个单独的线程来显示吐司?
Whether I need to create a separate thread to display Toast?
public class ClientActivity extends Activity {
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.EditText01);
message =(EditText) findViewById(R.id.EditText02);
connectPhones = (Button) findViewById(R.id.Button01);
}
public void Click1(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
private class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
while (connected) {
try {
if(i>5)
{
}
else
{
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
message1= message.getText().toString();
out.println(message1);
i=i+1;
Log.d("ClientActivity", "C: Sent.");
}
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
推荐答案
把
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(ClientActivity.this,"asdf",Toast.LENGTH_LONG).show();
}
});
这行之后
Log.d("ClientActivity", "C: Connecting...");
这篇关于如何显示处理器/线程内敬酒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文