This example describes the anonymous inner class and callback function usage of Android programming. Share to everyone for your reference, specific as follows:
We often use some anonymous internal classes in our Android development, and the callback functions in them, for example, when we set up a listener for the button, we usually implement the Onclicklistener interface and override the OnClick () method, which is the anonymous inner class and callback function, So how much do you understand about Java anonymous inner classes and callback functions? This document gives a more detailed explanation.
First you should know that the inner class is the main additional part of the Java language. An inner class can be anywhere within a class, at the same level as an instance variable, or within a method, or even as part of an expression. Java internal classes, divided into member internal classes, local internal classes, and anonymous internal classes, below the anonymous inner class.
A About callback functions
Anonymous inner class which uses a callback function, what is a callback function?
A callback is the client C calling a function in service s, and then S, in turn, calls a function B in C, which is called a callback function for C. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call B,c itself to provide b for the purpose of having s to invoke it, and C has to provide it. Since S does not know the B thy name provided by C, S will agree to B's interface specification (function prototype), and then by C, a function of S is passed ahead of it, which tells S that he is going to use the B function, which is called the registration of the callback function, R called the registration function. Both Web service and Java RMI Use callback mechanisms to access remote server programs. Here is an example to illustrate:
Programmer a wrote a program (program a) that reserved a callback function interface and encapsulated the program. Programmer B wants A to call a method in its own program B, so he recalls the method in B through the interface in a. Objective to achieve. In C + +, to use the callback function, the lost function needs to tell the caller his or her own pointer address, but there is no pointer in Java, what to do? We can implement the definition of callback functions through an interface (interface).
Assuming I'm programmer a, here's my program A:
public class Caller
{public
mycallinterface MC;
public void Setcallfuc (Mycallinterface mc)
{
this.mc= mc;
}
public void Call () {
this.mc.method ();
}
}
I also need to define an interface for programmer B to implement the interface based on my definition of programming.
Public interface Mycallinterface
{public
void method ();
}
As a result, programmer B can only implement this interface to achieve the purpose of the callback:
public class B implements Mycallinterface
{public
void method ()
{
System.out.println ("callback");
} Public
static void Main (String args[])
{
Caller call = new Caller ();
CALL.SETCALLFUC (New B ());
Call.call ();
}
B. About anonymous internal classes
To understand what an inner class is, how the inner class works, how Java is polymorphic, and what is a callback function. There are a lot of detailed explanations on the web, just for simplicity's sake. The anonymous inner class is passed to the method as a parameter of the function, in which the incoming anonymous class is accepted with the interface type, and then the method is called, which is polymorphic. The principle is actually to implement the callback. Because he will call the method in your anonymous inner class. The following code is tapped without testing for demonstration purposes only.
Interface interfacea{
String go ();
}
Class Test () {public
void Prtgo (Interfacea ia) {
System.out.println (IA.O ());
}
public static void Main (String []args) {
Test t = new test ();
T.prtgo (New Interfacea () {public
String go () {return
"Go";}}
);
}
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary
I hope this article will help you with the Android program.