在Android的使用活套。prepare()细节 [英] Specifics on using Looper.prepare() in Android

查看:156
本文介绍了在Android的使用活套。prepare()细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点很难理解如何使用尺蠖 prepare() / 循环() / 退出()逻辑。

I'm having a bit of trouble understanding how to use the Looper prepare()/loop()/quit() logic.

我有三个主题:一是UI线程,一个是游戏逻辑线程,最后是网络通信线程(后台线程,只生活在被使用)。

I have three threads: one is the UI thread, one is a game logic thread and the last is a network communication thread (a background thread, lives only while being used).

游戏线程对网络调用的结果许多依赖,所以我想旋转的网线断了游戏线程,并有一个处理程序后的结果背部。

The game thread has many dependencies on the results of the network calls, so I wanted to spin the network thread off of the game thread and have a Handler post the result back.

当然,因为UI线程不参与我需要调用活套。prepare() ...某处。我认为它应该被称为在游戏中的线程,但我不能这样做,因为循环()需要它了。

Of course, since the UI thread is not involved I need to call Looper.prepare()... somewhere. I thought it should be called in the game thread, but I can't do that because loop() takes it over.

我如何去回发到游戏线程网线与我的处理程序?

How do I go about posting back to the game thread from network thread with my handler?

推荐答案

这是怎么回事是,一旦你叫尺蠖。prepare(),其次是Looper.loop()的线程,所有的线程将曾经的做的是服务,它MessageQueue直到有人呼叫退出()就其活套。

What's going on is that once you call Looper.prepare() followed by Looper.loop() on a Thread, all that Thread will ever do is service its MessageQueue until someone calls quit() on its Looper.

另外要意识到的是,在默认情况下,当一个处理程序被实例化,这是code将始终有人在

The other thing to realize is that, by default, when a Handler is instantiated, it's code will always execute on the Thread it was created on

你应该做的是创建一个新的线程,并在run()调用活套。prepare(),设置的任何处理程序,然后调用Looper.loop()。

What you should do is create a new Thread and in run() call Looper.prepare(), setup any Handlers, and then call Looper.loop().

下面轴承考虑到这些事情是我用了很多地方的基本格局。此外,还有你应该只使用AsyncTask的,而不是一个很好的机会。

Bearing these things in mind here is the basic pattern I use a lot of places. Also, there's a good chance you should just be using AsyncTask instead.

public class NetworkThread extends Thread {
    private Handler mHandler;
    private Handler mCallback;
    private int QUIT = 0;
    private int DOWNLOAD_FILE = 1;
    public NetworkThread(Handler onDownloaded) {
        mCallback = onDownloaded;
    }

    public void run() {
        Looper.prepare();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    // things that this thread should do
                    case QUIT:
                        Looper.myLooper().quit();
                        break;
                    case DOWNLOAD_FILE:
                        // download the file
                        mCallback.sendMessage(/*result is ready*/);
                }
            }
        }
        Looper.loop();
    }

    public void stopWorking() {
        // construct message to send to mHandler that causes it to call 
        // Looper.myLooper().quit
    }

    public void downloadFile(String url) {
        // construct a message to send to mHandler that will cause it to
        // download the file
    }
}

这篇关于在Android的使用活套。prepare()细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆