Android中的消息注册Registrant机制

前面说过MessageHandle机制(见),它保证了Android上层代码线程间的通信。

消息注册机制更加丰富了MessageHandle的流程,它把过程严格阶段化了,分成消息注册和通知消息处理两个部分,让人一目了然。

消息注册机制的总体思想是:一个对象中开辟一个空间用于存放Message,当调用regist方法时将Message存放进去,当其调用notify方法时将所有Message取出并发送到MessageQueue中等待处理。

下面是模拟程序,提供Registrant,RegistrantList,AsyncResult,MiddleUser类

package com.zte.liu.registermessage;

public class AsyncResult {

MessageParmeters parmList;
 Object result;
 Throwable exception;
 
 public Throwable getException() {
  return exception;
 }

public void setException(Throwable exception) {
  this.exception = exception;
 }

AsyncResult(Object result, Throwable exception){
  parmList = new MessageParmeters();
  this.result = result;
  this.exception = exception;
 }
 
 public MessageParmeters getMessageParms(){
  return parmList;
 }
 
 public Object getResult(){
  return result;
 }
 
 public void setResult(Object result){
  this.result = result;
 }
 
 public AsyncResult refresh(Object parm){
  parmList.refresh(parm);
  return this;
 }
}

package com.zte.liu.registermessage;

import com.zte.liu.messagehandler.Handler;

public class MiddleUser {

RegistrantList registrantList = new RegistrantList();
 
 public void registForReason(Handler handler, int what, Object firstParms){
  registrantList.add(handler, what, firstParms);
 }
 
 public void notifyRegistrant(Object secondParms, Throwable exception){
  registrantList.notifyRegistrantList(secondParms, exception);
 }
 
 public void unRegistForReason(int what){
  registrantList.remove(what);
 }
}

package com.zte.liu.registermessage;

import com.zte.liu.messagehandler.Handler;
import com.zte.liu.messagehandler.Message;

public class Registrant {

private Message msg = null;
 
 Registrant(Handler handler, int what, Object firstParms){
  msg = new Message(what, handler, new AsyncResult(null, null).refresh(firstParms));
 }
 
 public void notifyRegistrant(Object secondParms, Throwable exception){
  if(msg == null){
   return;
  }
  AsyncResult asyncResult = (AsyncResult)msg.getObj();
  asyncResult.refresh(secondParms).setException(exception);
  msg.sendToTarget();
 }
 
 public Message getMessage(){
  return msg;
 }
}

package com.zte.liu.registermessage;

import java.util.ArrayList;

import com.zte.liu.messagehandler.Handler;

public class RegistrantList {
 
 private ArrayList<Registrant> registrantList = new ArrayList<Registrant>();
 
 public synchronized void add(Handler handler, int what, Object firstParms){
  Registrant registrant = new Registrant(handler, what, firstParms);
  registrantList.add(registrant);
 }
 
 public synchronized void remove(int what){
  for(int i=0; i<registrantList.size(); i++){
   if(((Registrant)registrantList.get(i)).getMessage().getId() == what){
    registrantList.remove(i);
   }
  }
 }
 
 public synchronized void notifyRegistrantList(Object secondParms, Throwable exception){
  for(int i=0; i<registrantList.size(); i++){
   Registrant registrant = (Registrant)registrantList.get(i);
   registrant.notifyRegistrant(secondParms, exception);
  }
 }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwyjyx.html