Today, I want to summarize some of my experiences with android over the past month. The biggest experience is the performance of callback functions in the Android listening mechanism. Android listeners are everywhere ~
I. Callback Functions
~ The so-called callback means that the client program C calls A function A in service program S, and then S calls A function B in C at A certain time. For C, this B is called a callback function. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call B by itself. C provides B for S to call it, and C has to provide it. Because S does not know who the B surname provided by C is, S will agree with the interface specification (function prototype) of B ), then C tells S to use the B function through a function R of S in advance. This process is called the registration of the callback function, and R is called the registration function. The callback mechanism is used by Web Service and Java RMI to access remote server programs.
The specific process is as follows:
Java does not have the pointer concept. The callback function is implemented through interfaces and internal classes:
1. Define the interface Callback, including the callback method Callback ()
2. Declare a Callback interface object mCallback in a class Caller
3. Assign the Caller object interface member (mCallback) An internal class object in the program, such
New Callback (){
Callback (){
// Function implementation
}
In this way, the callback () method can be called by the mCallback interface member of the Caller object as needed to complete the callback.
Ii. Instances
1. Define Interfaces
Public interface OnUpKeyLisnter {// define an excuse for others to Write Program Implementation interfaces according to my definition
Public void OnUpkey ();
}
2. Define service functions
Public class InterfaceServer implements OnUpKeyLisnter {
Public OnUpKeyLisnter mOnUpKeyLisnter;
@ Override
Public void OnUpkey (){
// TODO Auto-generated method stub
If (mOnUpKeyLisnter! = Null ){
MOnUpKeyLisnter. OnUpkey ();
}
}
Public void setOnUpKeyListener (OnUpKeyLisnter l ){
This. mOnUpKeyLisnter = l;
}
}
3. Define customer Functions
Public class InterfaceClient {
Public static void main (String... args ){
InterfaceServer mServer = new InterfaceServer ();
MServer. setOnUpKeyListener (new OnUpKeyLisnter (){
@ Override
Public void OnUpkey (){
// TODO Auto-generated method stub
System. out. println ("HAHAHA ~ ");
}
});
}
}
Callback functions are frequently used in Android development and must be thoroughly understood.
This article is from "Zeng Jinghai is difficult for water ~" Blog