When using the interface callback found a frequent error, that is, the implementation of the callback function is likely to be multi-threaded or asynchronous task to do, which will lead us to expect the function callback to return the result of a main function, the actual discovery is not feasible, Because if the callback is multithreaded, you can not synchronize with the main function, that is, the returned data is wrong, this is a very secret error. What is the best way to achieve linear transmission of data? First, we introduce the principle of callback mechanism.
callback function
A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
Detailed Explanation:
The client program C calls a function A in the service program s, and then S, at some point, calls a function B in C, and for C, this b is called a callback function. For example, a window procedure function under Win32 is a typical callback function. Generally speaking, C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide. Since s does not know B name provided by C, S will contract B's interface specification (function prototype), and then by C advance through S's a function R tells S itself to use the B function, this process is called the callback function registration, R is called the registration function. Both the Web service and the Java RMI use a callback mechanism to access the remote server program.
Here's a popular example:
one day, I call to ask you questions, of course, is a problem, you can not think of a solution, I can't hold the phone in there silly wait, so we agreed: Wait until you figure out a way to call me, so I hung up the phone to do other things. After xx minutes, my cell phone rang, you cheerfully said that the problem has been taken care of, should be treated as such. The story ends here. This example illustrates the programming pattern of asynchronous + callback. Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification.
The direct manipulation of pointers is not allowed in Java, so how does the callback be implemented?
Answer: It is implemented through an interface or an internal class.
Java method Callback is a method of function definition and function sharing, and it is a kind of coupling design idea. As an architecture, it must have its own operating environment and provide the user's implementation interface.
1. Define the interface Callback, including the callback method Callback ()
2. Declare a callback interface object in a class caller Mcallback
3. The interface member (MCALLBACK) that is assigned to the caller object in the program is an inner class object such as
New Callback () {
callback () {
specific implementation of the//function
}
}
This allows the Mcallback interface member of the caller object to invoke the callback () method, when needed, to complete the callback.
The use of callback mechanism in Android frame
in activity, many different states of the life cycle are defined to invoke the methods, which are empty implementations, the system framework to invoke, and the user to invoke to implement.
instance (for the Emulation of button click event Listener on Android interface):
A. Defining Interfaces
Public interface Onclicklistener {
Public void OnClick (Button b);
B. Defining a button
Public class Button {
Onclicklistener Listener;
Public void Click () {
Listener. OnClick (this);
}
Public void Setonclicklistener (Onclicklistener listener) {
This.listener = listener;
}
}
c. To assign an interface object Onclicklistener to the interface member of a button
Public class Activity {
Public Activity () {
}
Public static void Main (string[] args) {
button button = New button ();
Button.setonclicklistener (New Onclicklistener () {
@Override
Public void OnClick (Button b) {
System.out.println ("clicked");
}
});
Button.Click ();//user Click,system call Button.Click ();
}
}
Mechanism principle, that data transmission depends on how to pass the interface? On the code:
/ * * @author sunglasses * @category Use the Xutils framework to encapsulate the Get method, Ioauthcallback is the data interface callback, the Send method has an asynchronous task method, * The traditional return data method may return incorrect data. * /public class Xutilsget {public void Getjson (String url,requestparams params,final ioauthcallback ioauthcallback) { Httputils http = new Httputils (); Http.configcurrenthttpcacheexpiry (+); Http.send (httpmethod.get, URL, params, new requestcallback<string> () {@Overridepublic void OnFailure ( HttpException arg0, String arg1) {//TODO auto-generated method stub} @Overridepublic void Onsuccess (responseinfo< String> info) {//TODO auto-generated method Stubioauthcallback.getioauthcallback (Info.result);}});}}
I used the Xutils development framework and then returned the data using an interface callback, the interface is defined as follows:
Public interface Ioauthcallback {public void Getioauthcallback (String result);
The upper function is defined as follows:
public void Getcatajson (int cityid,ioauthcallback ioauthcallback) {String url = "Xxxxxxxxx"; Requestparams params = new Requestparams (); Params.addquerystringparameter ("Currentcityid", cityid+ ""); Getjson (url,params,ioauthcallback);}
Getcatajson is called in the class where you want the data, and then passed in parameters and interface objects, where the final data is processed. The principle of concrete mechanism is as above. This avoids the problem of multithreading data inconsistency in interface callbacks.
Reference: http://www.cnblogs.com/vtianyun/archive/2012/06/19/2555427.html
If you have any questions, please leave a message, reproduced the source.