线程不是在android中创建的

时间:2012-07-31 11:07:39

标签: android android-intent

我在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();
    }
}

当控件不能立即从thread返回时,我该怎么办?等到没有收到数据。

2 个答案:

答案 0 :(得分:0)

使用来自第二个线程的主线程我们必须在android中使用handler ... 离。

@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..

        }
    };

答案 1 :(得分:0)

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

或者,您可以使用

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

您可以使用Android multithreading guide非常有用。