【问题标题】:Message queue in androidandroid中的消息队列
【发布时间】:2011-08-18 12:34:41
【问题描述】:

谁能告诉我, 如何从消息队列中获取数据(消息)? 或者如何将消息从主线程发送到其他线程?。

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您想在线程上接收消息,您应该运行Looper 并创建一个绑定到此循环器的消息Handler。 UI线程默认有一个looper。有一个方便的类用于创建带有循环器的线程,称为HandlerThread。这是一篇关于 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);
    

    【讨论】:

      【解决方案2】:

      不是任何其他线程..您可以使用Handler.. 发送主线程或 Ui 线程。

      创建处理程序并发送一个可运行对象作为参数..

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2023-04-03
          • 2016-08-21
          • 1970-01-01
          • 2020-11-27
          • 2012-01-21
          • 2015-12-16
          • 2016-02-27
          • 2014-01-11
          • 2013-06-20
          相关资源
          最近更新 更多