2012-03-19 62 views
0

在一個活動我有個下面這段代碼:爲什麼在這種情況下處理程序只收到一條消息?

public void onStartMonitoringToggleClicked(View v) { 
    // Perform action on clicks 
    if (((ToggleButton) v).isChecked()) { 
     monitoringCanRun = true; 
     new Thread(new Runnable() { 
     public void run() { 
      performMonitoring(); //start the monitor 
     } 
     }).start(); 

    } else { 
     monitoringCanRun = false; 
    } 
    } 

    public void performMonitoring() { 
    while (monitoringCanRun) { 
     float parametervalue = Monitor.getParameterValue(); 

     //need to send the parameter value to the parameter screen's handler now 
     Message msg = mHandler 
      .obtainMessage(MESSAGE_UPDATE_PARAMETER_VALUE); 
     Bundle bundle = new Bundle(); 
     bundle.putFloat(PARAMETER_VALUE, parametervalue); 
     msg.setData(bundle); 
     mHandler.sendMessage(msg); 
     Log.i("x", "sending MSG------>"); //this gets called EVERY 4 seconds 
     try { //we delay to make debugging easier 
      Thread.sleep(4000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     } 

    } 


    public final int MESSAGE_UPDATE_PARAMETER_VALUE = 1; 
    public final String PARAMETER_VALUE = "paramVal"; 

    //the handler that receives the parameter values from the monitoring thread 
    public final Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     Log.i("x", "<-------RECEIVED MSG"); //this gets called only ONCE??????? 

     switch (msg.what) { 
     case MESSAGE_UPDATE_PARAMETER_VALUE: { 
     float parameter_value = msg.getData().getFloat(PARAMETER_VALUE); 
     if (DEBUG) 
      Log.i(this.getClass().getSimpleName(), 
       "-> " 
        + Thread.currentThread().getStackTrace()[2] 
         .getMethodName() 
        + " parameterValue=" 
        + String.format("%4.2", parameter_value)); 

     } 
     break; 

     } 

    } 
    }; 

我已經使用了相當的處理程序很多,但從來沒有過這樣的問題。這裏有什麼基本的錯誤嗎?

非常感謝

回答

0

您是否嘗試過不使用線程?你可以嘗試在每4秒鐘調用自己的地方使用處理程序嗎?我修改了一個項目,在那裏我使用Handler's來回叫。是的,我會在UI線程上,但它看起來不像你在做任何阻塞IO。我可能錯了。

public void onStartMonitoringToggleClicked(View v) { 
    // Perform action on clicks 
    if (((ToggleButton) v).isChecked()) { 
    monitoringCanRun = true; 
    mHandler.postDelayed(mUpdateTimeTask, 4000); 

    } else { 
    monitoringCanRun = false; 
    mHandler.removeCallbacks(mUpdateTimeTask); 
    } 
} 

private Runnable mUpdateTimeTask = new Runnable(){ 
    public void run(){ 
    mHandler.removeCallbacks(mUpdateTimeTask); 
    if(monitoringCanRun){ 
     mHandler.postDelayed(mUpdateTimeTask, 1000); 
     float parametervalue = Monitor.getParameterValue(); 

     Log.i(this.getClass().getSimpleName(), 
      "-> " 
       + Thread.currentThread().getStackTrace()[2] 
        .getMethodName() 
       + " parameterValue=" 
       + String.format("%4.2", parameter_value)); 
    } 
    } 
}; 

此外,爲什麼不把數據保存到全局共享並跨線程訪問它。我認爲你這樣做的方式你應該可以訪問線程數據。這又是一個想法。我希望這有幫助!

+0

有趣的想法 - 我會嘗試 – user387184 2012-03-19 22:35:41

+0

它爲你工作嗎? – Ethan 2012-03-20 17:38:48

+0

還沒有時間來測試 - 但是,我在我的代碼中發現了問題。只需檢查我的代碼中的最後一條語句 - 它缺少一個f格式選項。所以接收器剛剛「崩潰」並在第一次msg後進入調試模式 - 這是問題所在。不過,我仍然會嘗試你的建議...... – user387184 2012-03-20 22:25:29

相關問題