4

How to add a toast method inside a thread. I want to debug by replacing the system.out with a toast method to display results to the display.

I know that using the application Context from within the thread, like so: Toast.makeText(getApplicationContext(), "help", Toast.LENGTH_LONG).show(); will not work.

I don't know how to use the Runnable with the Toast call and calling runOnUiThread(runnable) from the Thread

Could someone help me out.

public class NetworkServer extends Thread { DatagramSocket mSocket = null; boolean isFinish = false; private SimplestPossibleActivity activity; public NetworkServer(SimplestPossibleActivity activity) { this.activity = activity; } public void run() { try { Log.d("UDP", "Listening"); mSocket = new DatagramSocket( 2010); //4444 mSocket.setBroadcast(true); while (!isFinish) { Log.d("UDP", "C: socket create success"); byte[] recvbuffer = new byte[12]; DatagramPacket packet = new DatagramPacket(recvbuffer,recvbuffer.length); Log.d("UDP", "receiving..."); mSocket.receive(packet); Log.d("UDP", "received packet"); ByteBuffer bb = ByteBuffer.allocate(recvbuffer.length).order(ByteOrder.LITTLE_ENDIAN); bb.put(recvbuffer); bb.rewind(); //System.out.println(bb.getFloat()); //System.out.println(bb.getFloat()); //System.out.println(bb.getFloat()); Bundle data = new Bundle(); data.putFloat("latitude", bb.getFloat()); data.putFloat("longitude", bb.getFloat()); data.putFloat("altitude", bb.getFloat()); Message msgHandle = new Message(); msgHandle.setData(data); mhandler.sendMessage(msgHandle); } //end while } catch (Exception e) { Log.e("UDP", "C: Error", e); } } private Handler mhandler = new Handler() { @Override public void handleMessage(Message msg) { Bundle data = msg.getData(); Log.d("NetworkServer","adding position" + "lat = " + data.getFloat("latitude") + "lon = " + data.getFloat("longitude") + "alt = " + data.getFloat("altitude")); activity.addPosition(data.getFloat("latitude"), data.getFloat("longitude"), data.getFloat("altitude")); } }; } 
2
  • you can use runOnuiThread. But runOnuiThread is a method of activity class. you cannot update ui on the background thread. ui should be updated on the ui thread Commented Aug 16, 2013 at 18:39
  • you cannot show toast on worker thread. You can only use Handler, runOnUiThread() or AsyncTask. Commented Aug 16, 2013 at 19:52

3 Answers 3

10

Use library Xdroid:

dependencies { compile 'com.shamanland:xdroid-toaster:0.2.4' } 

There are quite good approaches:

  1. Context variable is not required.
  2. runOnUiThread() is not required.

Just invoke the single method!

// using the resource string Toaster.toast(R.string.my_msg); // or hard-coded string Toaster.toast("Hello Xdroid!"); 

There are more examples here: https://github.com/shamanland/xdroid-toaster-example

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this
Handler handler = new Handler(); //Before your Thread

 //Within your thread handler.post(new Runnable(){ public void run() { Toast.makeText(getApplicationContext(), "help", Toast.LENGTH_LONG).show(); } }); 

1 Comment

This works only if the handler is started on the main thread.
0
 runOnUiThread(new Runnable() { @Override public void run() { try { //pick one: //if activity Toast.makeText(YOURACTIVITYNAME.this, "help", Toast.LENGTH_LONG).show(); //if fragment Toast.makeText(getActivity(), "help", Toast.LENGTH_LONG).show(); } catch (final Exception e) { } } }); 

1 Comment

bad idea to catch Exception and do nothing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.