为什么这种情况下的处理程序只接收一条消息?

时间:2012-03-19 21:52:15

标签: android multithreading event-handling

在一个活动中,我有以下代码:

   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;

        }

    }
    };

我使用过很多处理程序,但从来没有遇到过这种问题。这里有什么基本错误吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

您是否尝试过不使用该主题?您是否可以尝试使用每4秒调用一次自己的处理程序?我修改了一个项目,我使用Handler来做回调。是的,我会在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));
    }
  }
};

此外,为什么不将数据保存到全局共享中并跨线程访问它。我认为你这样做的方式你应该有权访问线程数据。这只是一个想法。我希望这有帮助!