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");
}
}
我正在使用HandlerThread
通过蓝牙发送数据。上面的代码在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 Thread中运行,我只是从这里向HandlerThread'
Looper
Queue
添加消息。
请您的想法
答案 0 :(得分: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();