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
请你的想法