0
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        if(seekBar.getId() == R.id.seekBar)
        {
            speed.setText(String.valueOf(progress));
            String outputData = speed.getText().toString()+","+"hey"+","+"hell"+";";

            if(streams.getOutputStream() != null && wtHandler != null)
            {
                wtHandler.sendMessage(wtHandler.obtainMessage(9999, outputData)); //wtHandler is Handler for HandlerThread's Looper

            }
            else
            {
                Log.d("RTR", "handler of output stream null");
            }
        }

我正在使用aHandlerThread通过蓝牙发送数据。上面的代码在SeekBar更改及其工作时传输数据。

当我从方法执行上述传输时onSensorChanged(),它不起作用。代码如下。

public void onSensorChanged(SensorEvent event) {
        //To change body of implemented methods use File | Settings | File Templates.
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        {
            float accXYZ[] = event.values;

            float accX = accXYZ[0];
            float accY = accXYZ[1];
            float accZ = accXYZ[2];

            x.setText(String.format("%.2f",accX));
            y.setText(String.format("%.2f",accY));
            z.setText(String.format("%.2f",accZ));

            //String outputData = x.getText().toString()+","+speed.getText().toString()+";";
            String outputData = x.getText().toString()+";";

            if(wtHandler != null && wtHandler.getOutputStream() != null)   //add output stream null later
            {

                wtHandler.sendMessageDelayed(wtHandler.obtainMessage(9999,outputData),10);
                Log.d("RTR","Added message to the Queue");
            }
            else if(wtHandler == null)
            {
                Log.d("RTR","Worker Thread Handler not created yet");
            }
            else
            {
                Log.d("RTR","OP Stream null");
            }

可能是什么问题?我怀疑它调用 onSensorChanged() 的速率。上面的所有代码都在 UI 线程中运行,我只是从这里向HandlerThread's添加消息。Looper Queue

请你的想法

4

1 回答 1

0

这就是我如何更改将消息添加到队列的速率并且现在一切正常

public void onSensorChanged(SensorEvent event) {
        //To change body of implemented methods use File | Settings | File Templates.
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        {
            float accXYZ[] = event.values;

            float accX = accXYZ[0];
            float accY = accXYZ[1];
            float accZ = accXYZ[2];

            x.setText(String.format("%.2f",accX));
            y.setText(String.format("%.2f",accY));
            z.setText(String.format("%.2f",accZ));

            try {
                //String outputData = x.getText().toString()+","+speed.getText().toString()+";";
                String outputData = x.getText().toString()+","+speed.getText().toString()+";";

                if( workerThread.getWtHandler() != null && workerThread.getWtHandler().getOutputStream() != null)   //add output stream null later
                {
                    if(System.currentTimeMillis() - time > 50)
                    {
                        workerThread.getWtHandler().sendMessage(workerThread.getWtHandler().obtainMessage(9999, outputData));
                        Log.d("RTR","Added message to the Queue");
                    }
                }
                else if(workerThread.getWtHandler() == null)
                {
                    Log.d("RTR","Worker Thread Handler not created yet");
                }
                else
                {
                    Log.d("RTR","OP Stream in Handler null");
                }
            }catch (Exception e){
                Log.d("RTR","Worker Thread not created yet");
            }
            time = System.currentTimeMillis();
于 2013-02-22T11:29:37.897 回答