Registrant mechanism for message registration in Android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.