如何烤面包片更换一个线程内的System.out [英] How to replace the system.out with Toasts inside a thread

查看:115
本文介绍了如何烤面包片更换一个线程内的System.out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何线程里面添加一个敬酒的方法。我想用一个敬酒的方法来显示结果显示替换的System.out调试。

我知道,使用的应用程序上下文的线程中,像这样: Toast.makeText(getApplicationContext(),帮助,Toast.LENGTH_LONG).show(); 不管用。

我不知道怎么用了Runnable与吐司通话,然后从线程中调用runOnUiThread(可运行)

有人能帮助我。

 公共类是NetworkServer继承Thread
{

   DatagramSocket的mSocket = NULL;
   布尔isFinish = FALSE;

   私人SimplestPossibleActivity活动;

   公众是NetworkServer(SimplestPossibleActivity活动)
   {
    this.activity =活动;
   }

   公共无效的run()
   {

      尝试
      {

        Log.d(UDP,听);
        mSocket =新的DatagramSocket(2010年); // 4444
        mSocket.setBroadcast(真正的);

        而(!isFinish)
        {

           Log.d(UDP,C:插座创造成功);
           byte []的recvbuffer =新的字节[12];
           DatagramPacket类包=新的DatagramPacket(recvbuffer,recvbuffer.length);
           Log.d(UDP,接收......);
           mSocket.receive(包);
           Log.d(UDP,接收的数据包);

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


           捆绑数据=新包();
           data.putFloat(纬度,bb.getFloat());
           data.putFloat(经度,bb.getFloat());
           data.putFloat(高度,bb.getFloat());

           消息msgHandle =新的Message();
           msgHandle.setData(数据);
           mhandler.sendMessage(msgHandle);

       } //结束时
     }赶上(例外五){
        Log.e(UDP,C:错误,E);
     }

   }

   私人处理程序mhandler =新的处理程序()
   {

        @覆盖
        公共无效的handleMessage(信息MSG)
        {

           捆绑数据= msg.getData();
           Log.d(是NetworkServer,添加位置+经纬度=+ data.getFloat(纬度)+
                                 结肠=+ data.getFloat(经度)+
                                 ALT =+ data.getFloat(高度));
           activity.addPosition(data.getFloat(纬度),
                               data.getFloat(经度),
                               data.getFloat(高度));

    }

   };
}
 

解决方案

使用库 Xdroid

 相关性{
    编译com.shamanland:xdroid-烤面包机:0.2.4
}
 

有相当不错的方法:

  1. 不需要上下文变量。
  2. runOnUiThread()不是必需的。

只需调用一个方法!

  //使用资源串
Toaster.toast(R.string.my_msg);
//或硬codeD字符串
Toaster.toast(你好Xdroid!);
 

有更多的例子在这里: https://github.com/shamanland/xdroid-toaster-例如

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

    }

   };
}

解决方案

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

这篇关于如何烤面包片更换一个线程内的System.out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆