如何使用线程内的Toasts替换system.out

时间:2013-08-16 18:36:32

标签: android

如何在线程中添加Toast方法。我想通过用toast方法替换system.out来调试,以显示结果。

我知道在线程中使用应用程序Context,如下所示: Toast.makeText(getApplicationContext(),“help”,Toast.LENGTH_LONG)。show(); 不管用。

我不知道如何在Toast调用中使用Runnable并从线程调用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"));

    }

   };
}

3 个答案:

答案 0 :(得分:7)

使用库Xdroid

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

有很好的方法:

  1. Context变量不是必需的。
  2. runOnUiThread()不是必需的。
  3. 只需调用单一方法!

    // 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

答案 1 :(得分: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();
            }
         });

答案 2 :(得分: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) {

            }
        }
    });