I have read almost every question related to this topic but they are all perfect until "ending" the thread.
public class LooperThread implements Runnable {
Handler UI_Handler;
IncomingHandler handler;
public LooperThread(Handler ui_handler)
{
UI_Handler = ui_handler;
handler = new IncomingHandler(this);
}
public Handler getHandler() {
return handler;
}
public void handleMessage(Message msg)
{
int wht = msg.what;
wht++;
UI_Handler.obtainMessage(wht).sendToTarget();
if (wht == 4)
Looper.myLooper().quit();
}
@Override
public void run() {
Looper.prepare();
Looper.loop();
}
static class IncomingHandler extends Handler {
private final WeakReference<LooperThread> mService;
IncomingHandler(LooperThread lprThread) {
mService = new WeakReference<>(lprThread);
}
@Override
public void handleMessage(Message msg)
{
LooperThread service = mService.get();
if (service != null) {
service.handleMessage(msg);
}
}
}
}
here is the code that I tried. And I get "main thread not allowed to quit" message. I just want to quit the thread at some point where a condition is met.