How to display a toast inside a handler / thread?

I want to display a toast after sending a message to the socket. After that, "Log.d (" ClientActivity "," C: Sent. ");"

Do 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; } } } 

}

+6
source share
4 answers

put

  runOnUiThread(new Runnable() { public void run() { Toast.makeText(ClientActivity.this,"asdf",Toast.LENGTH_LONG).show(); } }); 

after this line

  Log.d("ClientActivity", "C: Connecting..."); 
+17
source
 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 toast",Toast.LENGTH_SHORT); } }, 1000); 
+3
source

You cannot create a toast from within a stream. Since this new thread does not have access to getApplicationContext() , which you pass to it. You must somehow establish a connection with the user interface thread (i.e., the main thread). So whenever you want to fry something, do it in the handler.Post(Runnable) method. A handler is the middle man between the user interface thread and the individual thread you are using. All user interface operations must be performed in the handler.Post(Runnable) run() method.

So in your activity to show a toast do this:

 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(....); } }); 

Remember to declare and initialize the handler object in your main action (off-stream)

 handler=new Handler(); 
+2
source

First declare a global handler,

 Handler handler=null; 

Then create a handler in your onCreate () like this,

 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(); } } }; 

And now in your Runnable class add this line after "Log.d("ClientActivity", "C: Sent.");"

 handler.sendEmptyMessage(0); 

The problem you are facing is that you cannot update the user interface from runnable. Handlers connect you to the main user interface. Therefore, you need to pass the message to your handler from your Runnable.

+1
source

Source: https://habr.com/ru/post/915862/


All Articles