0

我在我的 onCreate 方法中创建线程。我通过线程上的 udp 接收数据。由于某种原因,它不起作用。

private void UdpThread() {
//   server will listen to one client
    try
    {
        Thread udpServerThread = new Thread()
        {
            @Override
            public void run()
            {       
                Log.i("rrr","receivePacket3");
                try
                {
                    while(true)
                    {
                        sleep(1000);
                    Log.i("rrr","receivePacket2");
                    String[] recieved = null;

                    DatagramSocket clientSocket = null;
                    InetAddress IPAddress = null;
                    try {

                        IPAddress = InetAddress.getByName("localhost");
                    } catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    byte[] receiveData = new byte[1500];
                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                    try {
                        clientSocket = new DatagramSocket(8086);
                        Log.i("rrr","receivePacket");
                        clientSocket.receive(receivePacket);

                        Log.i("rrr","receivePacket1");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    modifiedSentence = new String(receivePacket.getData());
                    System.out.println("in client receive:" + modifiedSentence);

                    Log.i("Received data",modifiedSentence);
                    clientSocket.close();

                    modifiedSentence =  modifiedSentence.trim();
                    Log.i("Received data",modifiedSentence);
                    }
                } catch (Exception e){
                    Log.e("UDP", "S: Error", e);
                }
            }
        };
        udpServerThread.start();
        //udpServerThread.setPriority(Thread.MAX_PRIORITY);

        Toast.makeText(this,modifiedSentence,Toast.LENGTH_LONG).show();

    }catch (Exception e) {
        // TODO: handle exception
        Log.e("eeeee",""+e);
        e.printStackTrace();
    }
}

当控制没有立即从线程返回时我应该怎么做。等到没有收到数据。

4

2 回答 2

0

要从第二个线程处理主线程,我们必须在 android 中使用处理程序...例如。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new Thread(new Runnable() {
           public void run() {
            try{
                Message myMessage = new Message();
                myMessage.obj = "SEND ANY DATA";
            }
            catch (JSONException e) {
            e.printStackTrace();
            }

            }).start();

在处理程序中......

 private Handler finishHandler1 = new Handler() {
        @Override 
        public void handleMessage(Message msg) {
            // do the UI things..

        }
    };
于 2012-07-31T11:18:14.163 回答
0

您必须创建并初始化一个 Handler 对象并在 MAIN 或 UI 线程中执行此操作。之后,您将能够从工作线程向处理程序发布消息和 Runnables。

或者,您可以使用

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)

您可能对 Android多线程指南也很有用。

于 2012-07-31T12:35:54.673 回答