Android callback mechanism instance !, Android callback
The specific implementation is to define interfaces in the class, and input parameters (or do not pass them) in the interface implementation method ).
Pass in the newly created interface in the call class and implement the unimplemented method.
Public class CallBackClass {// input the corresponding interface as the parameter public void huidiao (final runDate rundate) {// use a Thread to replace the system event new Thread () {int I = 0; @ Overridepublic void run () {super. run (); while (true) {I ++; // input the callback parameter rundate. hui ("no." + I + "Callback parameter! "); Try {sleep (5000);} catch (InterruptedException e) {e. printStackTrace ();}}}}. start () ;}// define the public interface runDate {public void hui (String str );}}
Input Interface Parameters in the main function:
public class test {public static void main(String [] args){CallBackClass callback = new CallBackClass();callback.huidiao(new CallBackClass.runDate() {@Overridepublic void hui(String str) {System.out.println(str);}});}}
Interface callback is also used in the next custom ListView.
JAVA callback mechanism
Basically, this is a callback mechanism. The interface passed in the method is implemented as an interface of the Listener. when calling the method, the instance of the specific interface implementation can also be passed in advance (usually called registering the Listener ). This situation is common in the event mechanism in Java Swing programming.
There are always some interfaces between software modules. In terms of calling methods, they can be divided into three types: Synchronous call, callback and asynchronous call.
Synchronous call: a blocking call, which is a one-way call that the caller must wait for the execution of the other party to complete;
Callback: A two-way call mode, that is, the called party also calls the interface of the other party when the interface is called;
Asynchronous call: a mechanism similar to a message or event, but its call direction is the opposite. When an interface service receives a message or an event, the customer is notified (that is, the customer's interface is called ).
Asynchronous call implementation is a situation of callback. For example, a method can be returned in advance, and a specific logical execution can be called back and forth by another thread after the execution is completed, notifies the caller.
How to Understand the callback mechanism and template mechanism in java
That is, the callback is reflected in the fact that your classmates call your number. This example is used to compare the callback mechanism in Ajax and the callback + template mechanism used by HibernateTemplate in Spring to make it easier to understand the callback mechanism. Ajax code: Java code functiontest {if (window. activeXObject) {xmlHttp = newActiveXObject (Microsoft. XMLHTTP);} elseif (window. XMLHttpRequest) {xmlHttp = newXMLHttpRequest ();} xmlHttp. onreadystatechange = callback; xmlHttp. open (...); XmlHttp. send (null);} functioncallback {} among them, xmlHttp. onreadystatechange = callback indicates that the callback function is called when the state changes, and this callback is a callback function. Analogy: When xmlHttp sends a request, it is equivalent to when you call your classmates. xmlHttp does not know when the state will change, and you do not know when your classmates will solve the problem. The Callback function is equivalent to your phone number. When the state changes, the callback function is called. After your classmates solve the problem, they will call you to notify you. The callback mechanism and template method of HibernateTemplate in Spring the source code of HibernateTemplaet is very complicated. We can write a simple code to simulate: Java code interfaceCallBack {publicvoiddoCRUD ();} publicclassHibernateTemplate {publicvoidexecute (CallBackaction) {getConnection (); action. doCRUD (); releaseConnection ();} publicvoidadd () {execute (newCallBack () {publicvoiddoCRUD () {System. out. println (execute the add operation ...);}});} publicvoiddelete () {execute (newCallBack () {publicvoiddoCRUD () {System. out. p Rintln (execute the delete operation ...);}});} publicvoidgetConnection () {System. out. println (get connection ...);} publicvoidreleaseConnection () {release connection ...);}} analogy: The execute method can reflect the template mode. Taking the add method as an example, calling the execute method is equivalent to calling your classmates, but your classmates say it takes time, just as the execute method needs to get the connection first, so you pass the CallBack Anonymous class to it, and after it gets connected, it will execute your add operation, which is equivalent to telling your classmates the phone number, he will call you again after solving the problem.