The embodiment of callback functions in the Android listening Mechanism

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.