usually in writing programs often encounter listeners, such as button click Listener, Key Listener and so on. The Listener in Android and the callback function in Java are the same concept, which is to define an interface in the underlying code to invoke the high-level code. So what is a callback function? On the Internet, " in Windows, programmers want the system DLL to call a method of their own writing, and then use the DLL's callback function (CALLBACK) interface to write the program, make it call, this is called callback." When you invoke an interface, you need to strictly follow the defined parameters and method calls, and you need to handle the async of the function, which will cause the program to crash. This explanation seems to be more difficult to understand, here is a simple example, programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. "As I understand it, I dug a hole in program A and waited for you to fill in the content in program B. Not filled? That's the pit .
First, define the callback function
- Create a class that includes a variable for an interface
Private CallBack CallBack; // the variables of the interface
- Create a declaration of an interface
Public Interface callback{ publicvoid out (); the method to be implemented inside the interface }
- Binding to an interface in a class (or getting a reference to an interface)
Public void Setcallback (CallBack CallBack) { this. CallBack = CallBack; } Public void out () { callback.out (); System.out.println ("This is the output of the Callerclass"); }
- the entire class
Public classCallerclass {PrivateCallBack CallBack;//declaration of the interface PublicCallerclass () {}; Public voidSetcallback (CallBack CallBack) { This. CallBack =CallBack; } Public voidout () {callback.out (); System.out.println ("This is the output of Callerclass."); } Public Interfacecallback{ Public voidOut ();//the method to be implemented in the interface } }
View Code
Second, call the callback function
I am here to bind the interface in activity, the actual code is as follows:
Public class extends Implements callerclass.callback{ private Callerclass Caller ...}
Inherited the callback interface in Callerclass, the Out method is as follows:
@Override publicvoid out () { // TODO auto-generated Method Stub System.out.println ("This is the output on the activity"); }
Finally, call the Out () method of Callerclass in OnCreate:
New Callerclass (); Caller.setcallback (this); caller. Out ();
Full code:
Public classMainactivityextendsActivityImplementscallerclass.callback{PrivateCallerclass Caller; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Caller=NewCallerclass (); Caller.setcallback ( This); Caller. Out (); } @Override Public voidout () {//TODO auto-generated Method StubSystem.out.println ("This is the output on the activity"); }}
Iii. Results
perfect~
The sense of interface is similar to the pointer in C + +, which is a reference to an address but does not generate memory. So we can give the program a way to execute it beforehand. More than that, interface in the design of the role of the model is more powerful, Factory mode, builder mode, the Observer mode is used to interface.
Android Simple App (iii)--add listeners to your program