Examples of how Java (Android) callback functions are used

Source: Internet
Author: User

The callback mechanism is often used in Android development, the most typical of which is the implementation of the control being triggered, simply, when the button is clicked, the system calls the OnClick method, and we register the Onclicklistener listener for the button, When the click is triggered,the OnClick method in the Onclicklistener will be called back, and we will be able to perform the appropriate action in it.

Here's a simple example of how callbacks are implemented:


A simple example of using a callback function

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. Objective to achieve.

The definition of a callback function is implemented in Java through an interface (interface).

The following is programmer A's program a

public class A {public    CallBack mcallback;     public void Setcallfuc (Mycallinterface MC) {   this.mc= mc;     }  public void Call () {    this.mCallBack.method ();    }    }   

Define an interface so that programmer B implements the interface based on my definition of programming.

Public interface CallBack {public    void method ();       

So, programmer B only needs to implement this interface to achieve the purpose of the callback:

public class B implements Mycallinterface {  //implement this interface @override public  void Method () {    System.out.println (" When program a calls the method, I'll say ");}  }  


Test method

Test method public static void Main (String args[]) {    A A = new A ();  Equivalent to the establishment of a and B contact A.setlistener (new B ()) through the callback interface;    Called the method, the interface method in B prints A.call ();     }    



*************************************************************************************************************** **************************************************************************

callback Function Classic Example

Use a Java callback function to implement a tool class that tests the run time of a function:

public class Testobject {   /**  * A method to be tested, a time-consuming loop is performed */public   static void TestMethod () {   int i= 0; i< 100000000; i++) {    }   }   /**  * A simple way to test the execution time of a method *   /public void Testtime () {   long begin = System.currenttimemillis (); Test start time   testMethod ();//test method   Long end = System.currenttimemillis ();//test End time   System.out.println ("[ Use time]: "+ (End-begin)); Print use time   } public     static void Main (string[] args) {   testobject test=new testobject ();   Test.testtime ();   }   }   

You see the Testtime () method, only "//test method" is needed to change, let's do a function to achieve the same function but more flexible:

First, set a callback interface:

Public interface CallBack {   //method for performing callback operations   void execute ();   }   <span style= "font-family:arial;" > </span>
Defining tool Classes

public class Tools {    /**  * Test function usage time by defining the Execute method of the CallBack interface  * @param callBack *  /public   void Testtime (CallBack CallBack) {   Long begin = System.currenttimemillis ();///Test start time   callback.execute ();///callback operation   Long end = System.currenttimemillis ();//test End time   System.out.println ("[Use time]:" + (End-begin)); Print usage Time   }   

Test method

public static void Main (string[] args) {   tools tool = new Tools ();   Tool.testtime (New CallBack () {   ///define Execute method public   void execute () {   //Here you can add one or more methods   to test the run-time Testobject.testmethod ();}}  );}





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.