我在我的onCreate方法中创建线程。我通过线程上的udp接收数据。由于某种原因,它不起作用。线程不是在android中创建
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.and返回,并等待,直到数据没有收到。
你是什么意思*“线程没有创建和控制跳转到敬酒”*? 'udpServerThread.start();'将启动线程并立即返回,而启动的线程并行运行,并且您的代码将继续执行到下一行。 – assylias 2012-07-31 11:13:11
问题不完整(我只是修复了语言,拼写和格式),您还需要共享日志。 – Siddharth 2012-07-31 11:13:51
@assylias然后为什么我没有通过udp获取数据,我已经在线程中编码。 – Rcp 2012-07-31 11:18:22