android中的消息队列

时间:2011-08-18 12:34:41

标签: android

任何人都可以告诉我, 如何从消息队列中获取数据(消息)? 或者如何从主线程向其他线程发送消息?。

由于

3 个答案:

答案 0 :(得分:9)

如果要在线程上接收消息,则应运行Looper并创建绑定到此循环器的消息Handler。默认情况下,UI线程有一个looper。有一个方便的类用于创建名为HandlerThread的loopers的线程。这是一篇关于Handlers和Loopers的好文章:Android Guts: Intro to Loopers and Handlers

修改

HandlerThread thread = new HandlerThread("Thread name");
thread.start();

Looper looper = thread.getLooper();
Handler handler = new Handler(looper) {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case SOME_MESSAGE_ID:
                // SOME_MESSAGE_ID is any int value
                // do something
                break;
            // other cases
        }
    }
};

handler.post(new Runnable() {
    @Override
    public void run() {
        // this code will be executed on the created thread
    }
});

// Handler.handleMessage() will be executed on the created thread
// after the previous Runnable is finished
handler.sendEmptyMessage(SOME_MESSAGE_ID);

答案 1 :(得分:0)

不是任何其他线程..你可以使用Handler来判断主线程或Ui线程..

创建处理程序并将an runnable对象作为anarguement发送..

答案 2 :(得分:0)

应用程序首次在设备上启动时

private final Handler handler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        if (WHAT == msg.what) {
            textView.setMaxLines(msg.arg1);
            textView.invalidate();
        } else if (WHAT_ANIMATION_END == msg.what) {
            setExpandState(msg.arg1);
        } else if (WHAT_EXPAND_ONLY == msg.what) {
            changeExpandState(msg.arg1);
        }
        super.handleMessage(msg);

    }
};

您可以使用各种技术在 android 中创建线程。

private void doAnimation(final int startIndex, final int endIndex, final int what) {

    thread = new Thread(new Runnable() {

        @Override public void run() {

            if (startIndex < endIndex) {
                // if start index smaller than end index ,do expand action
                int count = startIndex;
                while (count++ < endIndex) {
                    Message msg = handler.obtainMessage(WHAT, count, 0);

                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    handler.sendMessage(msg);
                }
            } else if (startIndex > endIndex) {
                // if start index bigger than end index ,do shrink action
                int count = startIndex;
                while (count-- > endIndex) {
                    Message msg = handler.obtainMessage(WHAT, count, 0);
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    handler.sendMessage(msg);
                }
            }

            // animation end,send signal
            Message msg = handler.obtainMessage(what, endIndex, 0);
            handler.sendMessage(msg);
        }
    });

    thread.start();
}
<块引用>

请注意:通常Activity中已经调用了prepare()和loop(),所以我们应该进行如下检查:

  if (Looper.myLooper() == null) {
  Looper.prepare();
}