As mentioned earlier, the MessageHandle mechanism ensures the communication between code threads in the upper android layer.
The message registration mechanism enriches the MessageHandle process. It strictly stages the process and divides it into two parts: Message registration and notification message processing.
The general idea of the Message registration mechanism is that an object opens up a space for storing messages. When the regist method is called, the Message is stored, when it calls the receivy method, It extracts all messages and sends them to MessageQueue for medium waiting for processing.
The following is a simulation program that provides the Registrant, RegistrantList, AsyncResult, and MiddleUser classes.
Package com. zte. liu. registermessage;
Public class AsyncResult {
MessageParmeters parmList;
Object result;
Throwable exception;
Public Throwable getException (){
Return exception; www.2cto.com
}
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 policyregistrant (Object secondParms, Throwable exception ){
RegistrantList. policyregistrantlist (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 policyregistrant (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 policyregistrantlist (Object secondParms, Throwable exception ){
For (int I = 0; I <registrantList. size (); I ++ ){
Registrant registrant = (Registrant) registrantList. get (I );
Registrant. policyregistrant (secondParms, exception );
}
}
}
From: liuyangsyouxiang Column