【问题标题】:why does the handler in this case only recieve one message?为什么在这种情况下处理程序只收到一条消息?
【发布时间】:2012-03-19 21:52:14
【问题描述】:

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

   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;

        }

    }
    };

我经常使用处理程序,但从未遇到过此类问题。这里有什么基本错误吗?

非常感谢

【问题讨论】:

    标签: android multithreading event-handling


    【解决方案1】:

    你试过不使用线程吗?您可以尝试使用每 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));
        }
      }
    };
    

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

    【讨论】:

    • 还没有时间测试 - 但是,我在代码中发现了问题。只需检查我代码中的最后一条语句 - 它缺少 f 格式选项。所以接收器只是“崩溃”并在第一个消息后进入调试模式——这就是问题所在。不过,我还是会试试你的建议……
    猜你喜欢
    • 2021-08-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多