Android callback function Summary

Source: Internet
Author: User
A callback function is a function written by others, but not called by others.
 
The message response function can be regarded as a callback function, because it allows the system to call the function at an appropriate time. However, the message response function is used to process messages. Therefore, a single message is used as a type. In essence, it is a callback function.
 
However, the callback function is not only a message response function. For example, in kernel programmingProgramSome callback functions are required. After a device reads and writes data, the system calls these callback functions to perform subsequent work.
 
Callback functions give programmers the ability to writeCodeIt can jump out of the normal program control flow and adapt to the specific running environment for execution at the right time.
Callback Function Summary

Definition given by others for callback:

The so-called callback means that object a calls a method B in another object B, and then B calls a function C in a at a time. For B, this C is called a callback function.
Callback is a two-way call mode, that is, the called party also calls the interface of the other party when the interface is called.

 

My understanding is similar to the combination of general classes. Object A calls a method B of object B. However, Method B is abstract and later bound.

 

Java callback code.

1. Define the callback interface.

 Package  Com. Smart; /**  * Define the callback Interface  */      Public     Interface  Callback {  Void  Execute ();}  Package  Com. Smart;  /**  * Define the callback Interface  */    Public   Interface  Callback { Void  Execute ();}  Package  Com. Smart;  /**  * Tool  */      Public     Class  Tools {  Public     Void  Test (callback ){  Long Begin = system. currenttimemillis (); // Test start time Callback.exe cute (); //  /Perform callback operations          Long End = system. currenttimemillis (); //  Test End Time System. Out. println ("[use time]:" + (end-begin )); //  Print usage time  }  Public     Static     Void  Main (string [] ARGs) {tools =New  Tools (); tools. Test (  New  Callback (){  Public     Void  Execute (){  //  A. Method (); the execution time of a method in Class.  //  B. Method (); the execution time of a method of test Class B. System. Out. Print ("Callback" );}});}}  Package Com. Smart;  /**  * Tool  */    Public   Class  Tools {  Public   Void  Test (callback ){  Long Begin = system. currenttimemillis (); //  Test start time Callback.exe cute (); //  /Perform callback operations         Long End = system. currenttimemillis (); //  Test End Time System. Out. println ("[use time]:" + (end-begin )); //  Print usage time  }  Public   Static   Void  Main (string [] ARGs) {tools = New  Tools (); tools. Test (  New Callback (){  Public   Void  Execute (){  //  A. Method (); the execution time of a method in Class.  //  B. Method (); the execution time of a method of test Class B. System. Out. Print ("Callback" ) ;}}) ;}Advantages: I personally think the advantage is mainly to separate the variable and unchanged parts of the Code, which greatly improves the reusability and scalability of the program. 
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.