Android:线程无法启动

问题描述:

我尝试构建一个应用程序来控制无人机PARROT。 为此,我在无人机和设备之间创建一个插槽,并将其放入一个线程中。 起初线程正确运行,无人机起飞,但现在(我没有触及我的代码),无人机不想起飞。我注意到该线程不会立即运行(而之前正确运行) 这里是我的代码与主题:Android:线程无法启动

String commande=""; 
byte[] cmdToByte; 
InetAddress IpDrone; 
DatagramSocket clientSocket; 
boolean etat; 
DatagramPacket sendPacketWithCmd; 
private byte[] ip = {(byte)192, (byte)168, (byte)1, (byte)1 }; 
boolean isRunning = true; 
private final static long TIME_SLEEP= 20; 

public void run() { 
     try { 
    IpDrone = InetAddress.getByAddress(ip); 
    } catch (UnknownHostException e1) { 

    etat = true; 
    } 

    try { 
    clientSocket = new DatagramSocket(); 
    } catch (SocketException e) { 

    etat = true; 
    } 

    while(isRunning){ 

    runControl(); 
    etat = true; 
    try { 
     Thread.sleep(TIME_SLEEP); 
    } catch (InterruptedException e) { 

     } 
    } 

    } 
public void runControl(){ 

commande = "AT*REF=1,290717696<LF>AT*REF=2,290717952<LF>AT*REF=3,290717696<LF>"; 

cmdToByte = commande.getBytes(); 


try { 

sendPacketWithCmd = new DatagramPacket(cmdToByte,cmdToByte.length,IpDrone,5556);      
clientSocket.send(sendPacketWithCmd); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    etat = true; 
      } 
etat = true; 
isRunning=false; 
clientSocket.disconnect(); 
clientSocket.close(); 
      } 

我知道有,但的AsyncTask我真的想用我自己的线程来做到这一点。 所以我想知道我的问题是什么,为什么我的线程无法启动(在主要活动中,我用start()运行我的线程)。 提前感谢您!

+0

可以ü请调整你的代码? – 2013-05-07 09:42:43

尝试使用线程这样的

new Thread(new Runnable() { 

     @Override 
     public void run() { 
      // put ur code here 

     } 
     }).start(); 
+0

我只是这样做,它仍然不起作用 – le2s 2013-05-07 10:04:06

+0

最近有类似的问题,我发现@Override失踪意外失踪。加回来,现在工作正常。 – 2015-09-21 11:40:49