I'm sure the solution to my problem will be one of those "duh" moments, but I'm stuck so any helpful hints would be gratefully received.
Problem: I have a UI with a background thread. The UI has a start/stop button to initiate and then stop a process in the thread. This is achieved using messages from the UI to the message handler in the thread wrapped in a looper. The thread process is in a while loop dependent on a boolean set true/false by the thread message handler. However, once started, no further messages are processed and the loop continues ad infinitum.
private class MyThread extends Thread{
public void run() {
Looper.prepare();
MyThreadChildHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.arg1) {
case START:
boolLoop = true;
doWhileLoop();
case STOP:
boolLoop = false;
}
};
Looper.loop();
}
private void doWhileLoop(){
while (boolLoop = true){
stuff.do
}
finish.do
}
}
I've got a logcat message on the STOP message from the UI and that's firing, but the thread never picks it up because it's stuck in the while loop and the message handler doesn't get a look-in.
Is there a way of letting the thread handler check for messages from within the while loop at various points? Or is there another way of getting a thread based continuous while loop to start and stop based on messages from the UI?
Cheers Andy