0
我'与新的线程,所以我读到的一篇文章/教程... tutorial
我的目标是更新的TextView从线程的GUI ...
public class recorderThread extends Thread {
public boolean recording = true; //variable to start or stop recording
public int frequency; //the public variable that contains the frequency value "heard", it is updated continually while the thread is running.
int numSamples;
AudioRecord recorder;
int numCrossing,p;
short audioData[];
int bufferSize;
private Handler parentHandler;
public recorderThread (Handler parentHandle) {
this.parentHandler = parentHandle;
}
@Override
public void run() {
doingSuff();
Message msgn = new Message() ;
msgn.arg1 = frequency;
/* Sending the message */
parentHandler.sendMessage(msgn);
} //while recording
}//run
}//recorderThread
而且活动
public class sonicDistance extends Activity {
recorderThread rthread ;
static TextView freq;
static TextView duration;
static TextView samplerate;
private static TextView temperature;
static TextView measuredDistance;
private static TextView measuredTimeDiff;
public static TextView measuredFrequenz;
private CustomDialog myfreqDialog;
static double startTime = 0;
static double endTime = 0;
private static float c_propagation = 0;
private static float choosen_temperature = 20;
static double messureFreq = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
freq =((TextView) findViewById(R.id._freqOfTone));
duration =((TextView) findViewById(R.id.duration));
samplerate = ((TextView) findViewById(R.id.Samplerate));
temperature = ((TextView) findViewById(R.id.temperature));
measuredFrequenz = ((TextView) findViewById(R.id._messuredFrequenz));
measuredDistance = ((TextView) findViewById(R.id.distance));
measuredTimeDiff = ((TextView) findViewById(R.id._timediff));
rthread = new recorderThread(myHandler);
rthead.run();
}
public Handler myHandler = new Handler(){
public void handleMessage(Message msg) {
System.out.println(msg.arg1);
setTemperature(Float.valueOf((String)msg.obj));
System.out.println("test" + Float.valueOf((String)msg.obj));
}
};
@Override
protected void onResume(){
super.onResume();
}
}
处理程序的活动中没有system.out。我认为这个从未被调用过。
我不知道哪里是我的错..
感谢您
嗯,我尝试Log.d(...),和调试模式,但没有。看起来handleMessage()永远不会被调用。 setTemperature()fkt。并不重要(但我也改变了这一部分)。 Damm我不知道错误在哪里.. – marcel 2011-05-04 18:17:21
我发现我的错误,我用.run启动线程而不是.start() – marcel 2011-05-05 13:24:18