4

如何在线程中添加 toast 方法。我想通过用 toast 方法替换 system.out 来将结果显示到显示器来进行调试。

我知道从线程内使用应用程序上下文,如下所示: Toast.makeText(getApplicationContext(), "help", Toast.LENGTH_LONG).show(); 不管用。

我不知道如何将 Runnable 与 Toast 调用一起使用并从 Thread 调用 runOnUiThread(runnable)

有人可以帮帮我。

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"));

    }

   };
}
4

3 回答 3

10

使用库Xdroid

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

有很好的方法:

  1. Context变量不是必需的。
  2. runOnUiThread()不需要。

只需调用单个方法!

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

这里还有更多示例:https ://github.com/shamanland/xdroid-toaster-example

于 2013-08-16T18:59:08.003 回答
1

你可以这样做
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();
            }
         });
于 2013-08-16T18:41:45.993 回答
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) {

            }
        }
    });
于 2013-08-16T19:47:20.693 回答