0

我已经阅读了几乎所有与该主题相关的问题,但在“结束”线程之前它们都是完美的。

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);
            }
        }
    }
}

这是我尝试过的代码。我收到“不允许退出主线程”的消息。我只想在满足条件的某个时候退出线程。

4

0 回答 0