本文介绍了Android 2.1:单个活动中有多个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中有多个Handlers.我在主活动的onCreate()中创建所有处理程序.我的理解是,永远不会在同一时间调用每个处理程序的handleMessage()方法,因为所有消息都放在同一队列(活动线程MessageQueue)中.因此,它们将按照放入队列的顺序执行.它们还将在主活动线程中执行.这样对吗 ?

I've more than one Handlers in an Activity. I create all the handlers in the onCreate() of the main activity. My understanding is the handleMessage() method of each handler will never be called at the same time because all messages are put in the same queue (the Activity thread MessageQueue). Therefore, they will be executed in the order in which are put into the Queue. They will also be executed in the main activity thread. Is this correct ?

 public void onCreate() {

this.handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {

                            //operation 1 : some operation with instanceVariable1
super.handleMessage(msg);
}
};

this.handler2 = new Handler() {

@Override
public void handleMessage(Message msg) {
                            //Operation 2: some operation with instanceVariable1
super.handleMessage(msg);
}

};

this.handler3 = new Handler() {
@Override
public void handleMessage(Message msg) {
                            //Operation 3: some operation with instanceVariable1
super.handleMessage(msg);

}
};
}

推荐答案

每次,每个线程,每个处理程序一条消息.

One message per time, and per thread, and per handler.

  1. 每个新的Handler(...)实例都明确地或隐式地绑定到Looper实例,并且仅绑定一次.
  2. Looper实例已通过Looper.prepare()调用在某处创建//通常由Activity.getMainLooper()或Looper.myLooper()获取
  3. Looper.prepare()使用ThreadLocal变量sThreadLocal(静态字段)使每个线程具有一个Looper实例.(其工作方式类似于hashMap.put(Thread.getCurrent(),new Looper()))
  4. 每个循环程序都有自己的私有MessageQueue
  5. 每个Looper实例都有其主要方法loop()

  1. Every new Handler(...) instance is bound, explicitly or implicitly, to Looper instance, and only once.
  2. Looper instance, is already created somewhere with Looper.prepare() call//usually obtained by Activity.getMainLooper() or Looper.myLooper()
  3. Looper.prepare() uses ThreadLocal variable sThreadLocal (static field) to have one Looper instance per thread.( It works same like hashMap.put(Thread.getCurrent(), new Looper()) )
  4. every looper has its own private MessageQueue
  5. every Looper instance has its main method loop()

loop(){
    while(true){
        Message msg = messageQueue.next();
        msg.target.dispatchMessage(msg);
    }
}

  • 每个消息都设置了(处理程序)目标,如果没有,则抛出异常(在MessageQueue.enqueueMessage()内).

  • every message have a (Handler) target is set, and exception is thrown(within MessageQueue.enqueueMessage()) if it does not.

    因此sendMessage()或postMessage()的工作方式如下:

    so sendMessage() or postMessage() works something like this:

        handler.post(Message msg){
            Looper.sThreadLocal.get(Thread.getCurrent()).messageQueue.push(msg);
        }
    

    因此,在处理消息时,调用stack应该看起来像这样:

    so call stack , while handle message, should look something like this:

    Looper.myLooper()-> Thread.getCurrent()-> Looper-> MessageQueue.next()-> Message-> Message.target-> Handler-> dispatchMessage()-> handleMessage()
    

    这篇关于Android 2.1:单个活动中有多个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 06-14 20:26