如何在处理程序/线程中显示Toast?

时间:2012-05-16 01:43:54

标签: android toast

我希望在将消息发送到套接字后显示一个Toast。之后 “Log.d(”ClientActivity“,”C:已发送。“);”

我是否需要创建一个单独的线程来显示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;
        }
    }
}

}

4 个答案:

答案 0 :(得分:18)

  runOnUiThread(new Runnable() {
                 public void run() {

                     Toast.makeText(ClientActivity.this,"asdf",Toast.LENGTH_LONG).show();
                }
            });

这一行之后

  Log.d("ClientActivity", "C: Connecting...");

答案 1 :(得分:3)

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        System.out.println("I'm in handler");
        Toast.makeText(YourActivity.this, "This is a toast", Toast.LENGTH_SHORT).show(); 
    }
}, 1000);             

答案 2 :(得分:2)

您无法在线程内创建Toast。因为这个新线程无法访问您传递给它的getApplicationContext()。你必须以某种方式与UI线程(即主线程)进行交流。所以每当你想要在handler.Post(Runnable)方法中做某事时都要做。 Handler是UI线程和您正在运行的单独线程之间的middle man。所有UI操作都必须使用handler.Post(Runnable)的{​​{1}}方法完成。

所以在你的活动中展示吐司做这个:

run()

不要忘记在主活动中(在线程外部)声明并初始化一个处理程序对象

private class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
             .....
             .....
              message1= message.getText().toString();
                        out.println(message1);
                        i=i+1;
                        Log.d("ClientActivity", "C: Sent.");
                        handler.post(new Runnable(){
                          public void run()
                          {
                             Toast.make(....);
                           }
                         });

答案 3 :(得分:1)

首先声明一个全局处理程序,

Handler handler=null;

然后在onCreate()中创建一个像这样的处理程序,

Handler handler=new Handler()
{
  public void handleMessage(Message msg)
{
  if(msg.what==0)
{
   Toast.makeText(ClientActivity.this,"Your Toast Mesage Goes here",Toast.LENGTH_LONG).show();
}

}
};

现在在您的Runnable类中,在"Log.d("ClientActivity", "C: Sent.");"

之后添加此行
handler.sendEmptyMessage(0);

您遇到的问题是因为您无法从runnable更新UI。处理程序将您连接到主UI。所以你必须从Runnable向你的处理程序传递一条消息。