Bo Master is a junior college students, learning computer on the road, some of their own sentiment, write down to discuss with each other, welcome to spit Groove.
There is always a certain interface between the Java module and the module, which can be divided into three categories: synchronous invocation, callback and asynchronous invocation. Synchronous invocation is a kind of blocking call, which is also often used in writing programs; callbacks are a two-way invocation pattern, which means that the interface invoked by the called interface is called, and an asynchronous invocation is a mechanism similar to a message or event that solves the problem of synchronous blocking. Callbacks are fundamental to asynchrony, so the following emphasis is on the callback mechanism.
I understand that the Android callback function implements a callback function (usually an interface) in Class B (typically a class that is time consuming for the service class), and a callback interface for Class B in Class A (UI Class). Class A starts with the active registration of the callback function (Setcallback ()) The instantiation of the callback function is submitted to the caller. When the condition is met, the caller invokes the callback function through the interface to handle the event. after instantiating the class B is to use this method is my incoming pointer, then that is called my incoming data This also explains why the callback mechanism is asynchronous.
Overall three elements 1, Class A (UI Class) 2, Class B (service Class) 3, conventions (interfaces)
!!!! Below is very important very essence!!!!
A callback method is a method (interface) that is invoked through a method pointer. If I pass a pointer (address, reference) of this method through a parameter to a method of another class (Setcallback registration), then I can invoke the method on my side when the class invokes the pointer I passed in the method.
The whole process is I pass in the address of method A, you receive, and at some point callback my side of a method. since Java has no way to manipulate pointers, it is implemented with interfaces.
The Java method callback is a kind of coupling design idea. As an architecture, it must have its own operating environment and provide the user's implementation interface.
The main steps are as follows:
- 1, define the interface Callback, including the callback method Callback ();
- 2, Class B provides the method of registration, and sets a callback interface method under a certain condition;
- 3. Class A provide the implementation of the interface, and register to Class B, the method address to pass in.
This allows the Camcallback interface member to invoke the callback () method when needed to complete the callback.
Specific implementation code:
public class Bservice { private onparsercallback callBack; The Class B defines the interface definition callback method public interface Onparsercallback {public void Onparsercomplete (List
Android callback mechanism