我试图在Thread之后显示setText值magX, mahY, magZ and teslaXYZ
,但是当我评论Thread.sleep(10 * 1000);
时,没有显示任何内容,并且try catch块中的所有值都显示在act活动中。有人可以解释我的错误吗?
我添加了线程来延迟传感器事件读取
我赞成任何帮助。
代码:
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
try {
Thread.sleep(10 * 1000);
float magX = sensorEvent.values[0];
float magY = sensorEvent.values[1];
float magZ = sensorEvent.values[2];
magneticX.setText(Float.toString(sensorEvent.values[0]));
magneticY.setText(Float.toString(sensorEvent.values[1]));
magneticZ.setText(Float.toString(sensorEvent.values[2]));
double teslaXYZ = (Math.sqrt((magX * magX) + (magY * magY)
+ (magZ * magZ)));
magneticT.setText(Double.toString(teslaXYZ));
try {
JSONObject tesla = new JSONObject();
tesla.put("tesla", teslaXYZ);
telsaString = tesla.toString();
new MyAsyncTask().execute(telsaString);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
magnetic.post(new Runnable(){
public void run(){
magneticX.setText(Float.toString(sensorEvent.values[0]));
magneticY.setText(Float.toString(sensorEvent.values[1]));
magneticZ.setText(Float.toString(sensorEvent.values[2]));
double teslaXYZ = (Math.sqrt((magX * magX) + (magY * magY)
+ (magZ * magZ)));
magneticT.setText(Double.toString(teslaXYZ)); } }