简体   繁体   English

如何为我刚刚创建的新线程获取 Looper?

[英]How do I get the Looper for a new Thread that I just created?

I'm creating a thread to handle a request.我正在创建一个线程来处理请求。 In the thread I must call Looper.prepare() which is require by some other functionality it is using, and then I call Looper.loop() .在线程中,我必须调用Looper.prepare() ,这是它正在使用的其他一些功能所需要的,然后我调用Looper.loop() But when the connection for the request is closed, I get a callback in a different thread.但是当请求的连接关闭时,我会在不同的线程中收到回调。 At that point I need to call quit() on the Thread's looper so that loop() will return and the thread will exit.那时我需要在线程的循环器上调用quit()以便 loop() 将返回并且线程将退出。 However, I am having a hard time figuring out how to get that thread's looper.但是,我很难弄清楚如何获得该线程的循环器。 I don't see any in Thread to get the Looper for the thread.我在Thread没有看到任何可以获取Thread的 Looper。 In the Looper class I see a way to get the looper for the current class, but I'm not able to pass this information from the inner class of the Thread to the class that creates it:Looper类中,我看到了一种获取当前类的Looper的方法,但是我无法将此信息从 Thread 的内部类传递给创建它的类:

                final Looper l1;
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        Looper.prepare();
                        l1 = Looper.myLooper(); // ERROR!
                        // initialization goes here
                        Looper.loop();
                        Log.d(logtag,"The request thread is done.");
                    }
                };
                thread.start();

and in the callback which immediately follows the above line:并在紧随上述行之后的回调中:

webSocket.setClosedCallback(new CompletedCallback() {
    @Override
    public void onCompleted(Exception ex) {
        // other code
        l1.quit();
    }
});

I get a compile time error on the line marked above:我在上面标记的行上收到编译时错误:

Cannot assign a value to final variable 'l1'.

But I haven't assigned it yet!但是我还没分配呢! If I don't make Looper final, then I can't assign to it because it's not final.如果我不让Looper最终版本,那么我就不能分配给它,因为它不是最终版本。

This seems like somewhat of a hack, but I created this class (private, in the same file):这看起来有点像黑客,但我创建了这个类(私有的,在同一个文件中):

class LooperHolder {
    Looper l1 = null;
    void setLooper(Looper looper) {
        l1 = looper;
    }
    Looper getLooper() {
        return l1;
    }
}

Then I modified my code to go through the looper holder:然后我修改了我的代码以通过活套支架:

            final LooperHolder lh = new LooperHolder();
            final Thread thread = new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    lh.setLooper(Looper.myLooper());
                    // initialization goes here
                    Looper.loop();
                    Log.d(logtag,"The request thread is done.");
                }
            };
            thread.start();

And then in the callback:然后在回调中:

Looper l1 = lh.getLooper();
if (l1 != null) l1.quit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图在屏幕上显示一个CountDownTimer,我在我的logcat中得到“每个线程只能创建一个looper” - I am trying to display a CountDownTimer on screen and I get “Only one looper can be created per thread” in my logcat 如何在requestLocationUpdates中使用Looper? - How do I use Looper within requestLocationUpdates? 为什么handleMessage在ui线程中运行当我使用后台线程循环程序创建处理程序时? - why handleMessage run in ui thread While I created handler with background thread looper? 我应该将新线程作为扩展Thread的类创建还是仅执行新的Thread对象实例化? - Should I create a new thread as a class that extends Thread or just do a new Thread object instantiation? 为什么我得到一个“无法使用android.os.CountDownTimer在没有调用Looper.prepare()的线程中创建处理程序”? - Why do I get a “Can't create handler inside a thread that has not called Looper.prepare()” with android.os.CountDownTimer? 如何将我的主线程与创建的工作线程连接起来? - How do i connect my main thread with a created worker thread? 为什么我的AsyncTask中需要Looper? - Why do I need a Looper in my AsyncTask? 如何自动装配Spring TaskExecutor创建的线程? - How do I autowire a Spring TaskExecutor created thread? 当未打开应用程序并且尝试在Realm中插入数据时,出现错误“无法在线程上自动更新Realm>没有Looper”? - When app is not opened and when i try to insert Data in Realm I get Error “Realm cannot be automatically updated on a thread > without a looper”? 使用圆制作屏幕保护程序。如何为圆制作循环,以便创建新的圆? - Making a screensaver using circles.. How do i make a loop for circles so that new circles get created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM