2012-08-27 88 views
2

我的線程無法正常工作。它每3秒執行RepeatingThread()方法中的所有代碼,但不執行run()方法中的所有代碼。我究竟做錯了什麼?線程無法正常工作 - Android

這裏是代碼:

public RepeatingThread rt; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Log.e(LOG_TAG, "Start Repeat Thread"); 
    //rt = new Thread(new RepeatingThread()); 
    rt = new RepeatingThread(); 
    rt.start(); 
    Log.e(LOG_TAG, "Started Repeat Thread"); 
} 

public class RepeatingThread implements Runnable { 

    private final Handler mHandler = new Handler(); 
    private int len = 0; 


    private byte[] input = new byte[len]; 


    public RepeatingThread() { 
     //try { 
     Log.e(LOG_TAG, "Before inputJSON String"); 
     //inputJSON = dataInputStream.readUTF(); 
     //URL url = new URL("tcp://23.23.175.213:1337"); 
     //inputJSON = dataInputStream.readUTF(); 
     //inputstrrd = new InputStreamReader(socket.getInputStream()); 
     String hello = "hello world"; 
     //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream())); 

     //Convert 
     Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
     /*} 
     catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }*/ 

     //LOOK HERE FIRST 
     //inputJSON is what is received back from the server - Take the inputJSON 
     //String and use regular expressions HERE to remove all the other characters in the 
     //string except the payload JSON. 
     //refreshViewModels(inputJSON); 
    } 

    @Override 
    public void run() { 
     try { 
      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON"); 
      //outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK 
      //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr)); 
      inputJSON = ConvertByteArrayToString(getFileBytes(inputStr)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON); 
     refreshViewModels(inputJSON); 
     mHandler.postDelayed(this, 3000);  
    } 
} 
+0

難道refreshViewModels()拋出一個異常? –

+0

我也想過,但它甚至沒有達到那裏,因爲在REPATTINGTHREAD-INPUTJSON沒有被打印出logcat。所以我不認爲這是問題。 – user268397

回答

1

你應該使用一個Timer對於這種短期反覆任務:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TimerTask task = new RepeatingTask(); 
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(task, 0, 3000); 
} 

public class RepeatingTask extends TimerTask { 

private int len = 0; 

private byte[] input = new byte[len]; 


public RepeatingTask() { 
    //try { 
     Log.e(LOG_TAG, "Before inputJSON String"); 
     //inputJSON = dataInputStream.readUTF(); 
     //URL url = new URL("tcp://23.23.175.213:1337"); 
     //inputJSON = dataInputStream.readUTF(); 
     //inputstrrd = new InputStreamReader(socket.getInputStream()); 
     String hello = "hello world"; 
     //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream())); 

     //Convert 
     Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
    /*} 
    catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }*/ 

    //LOOK HERE FIRST 
    //inputJSON is what is received back from the server - Take the inputJSON 
    //String and use regular expressions HERE to remove all the other characters in the 
    //string except the payload JSON. 
    //refreshViewModels(inputJSON); 
} 

@Override 
public void run() { 
     try { 
      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON"); 
      //outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK 
      //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr)); 
      inputJSON = ConvertByteArrayToString(getFileBytes(inputStr)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON); 
     refreshViewModels(inputJSON); 
} 

}

在代碼中,你混的線程模式(從start()方法開始)和處理程序,這有點令人困惑。

+0

但是,我如何從onCreate()開始呢? – user268397

+0

我使用原始代碼編輯我的答案 – fiddler

+0

我試過你的代碼,但由於某種原因run()方法中的代碼沒有執行?你有什麼想法可能會造成這種情況? – user268397

2

您需要延長線程,而不是實現Runnable接口:

public class RepeatingThread extends Thread{ 
//..... 
}