Callback Mechanisms in Java

Source: Internet
Author: User

Previously did not understand what called the callback, every day listen to people say add a callback method What, my heart think I grass, what is called callback method ah? And then I find it on the Internet Ah find, find a lot is not very clear, now know, called callback: is a class called in Class B of a method C, and then in the Class B in turn called the method in Class A d,d this method called callback method, so that you are not a bit dizzy, in fact, I just started to understand , looked at the other people say the more classic callback method:

    • Class a implements interface callback callback--background 1

    • Class A contains a reference to Class B b--background 2

    • Class B has a method with a parameter of CallBack F (CallBack CallBack)--Background 3

    • Object A of a calls B method F (CallBack CallBack)--a class calls a method of Class B

    • Then B can call a method in the F (CallBack CallBack) method--b class calls a method of Class D

We all like to use the phone example, well, in order to keep up with the times, I also use this example, I use this example of asynchronous plus callback

One day Xiao Wang encountered a difficult problem, the question is "1 + 1 =?", on the phone to ask Xiao Li, Xiao Li suddenly did not know, and Xiao Wang said, and so I finish the things on hand, to think about the answer, Xiao Wang will not be silly to take the phone to wait for Xiao Li's answer, so Xiao Wang said to Xiao Li, I have to go shopping, You know the answer to call me to tell me, so hang up the phone, do their own things, after one hours, Xiao Li hit the small Wang's phone, told him the answer is 2

/**

* This is a callback interface

* @author xiaanming

*

*/

Public interface CallBack {

/**

* This is the function that Xiao Li will call when he knows the answer. Tell Xiao Wang, the callback function

* @param result is the answer

*/

public void Solve (String result);

}

/**

* This is Xiao Wang

* @author xiaanming

* Implemented a callback interface callback, equivalent to-----> background A

*/

public class Wang implements CallBack {

/**

* Reference to Xiao Li object

* Equivalent to-----> Background II

*/

Private Li Li;

/**

* The construction method of Xiao Wang, holding the reference of Xiao Li

* @param li

*/

Public Wang (Li Li) {

This.li = Li;

}

/**

* Xiao Wang used this method to ask Xiao Li's question.

* @param question is the question that Xiao Wang wants to ask, 1 + 1 =?

*/

public void Askquestion (final String question) {

Here a thread is asynchronous,

New Thread (New Runnable () {

@Override

public void Run () {

/**

* Xiao Wang calls the method of small Lee, register callback interface here

* This is equivalent to method C of Class A call B

*/

Li.executemessage (wang.this, question);

}

}). Start ();

The net asks the question to hang up the telephone to do other things, exaggeration street.

Play ();

}

public void Play () {

System.out.println ("I'm going to go shopping");

}

/**

* Xiao Li know the answer after calling this method to tell Xiao Wang, is the so-called Xiao Wang's callback method

*/

@Override

public void Solve (String result) {

System.out.println ("Xiao Li told Xiao Wang the answer is--->" + result);

}

}

/**

* This is Xiao Li.

* @author xiaanming

*

*/

public class Li {

/**

* Equivalent to Class B with parameters of callback callback F ()----> Background Three

* @param callBack

* @param question Wang asked the question

*/

public void ExecuteMessage (CallBack CallBack, String question) {

System.out.println ("Xiao Wang asked questions--->" + question);

It takes a long time to simulate Xiao Li's own affairs.

for (int i=0; i<10000;i++) {

}

/**

* When Xiao Li finishes his own business, he thinks the answer is 2.

*/

String result = "answer is 2";

/**

So I called Xiao Wang and called Xiao Wang's method

* This is equivalent to Class B, which in turn calls a method D

*/

Callback.solve (result);

}

}

/**

* Test class

* @author xiaanming

*

*/

public class Test {

public static void Main (String[]args) {

/**

* New A little Lee

*/

Li li = new Li ();

/**

* New A Little King *

*/

Wang Wang = new Wang (LI);

/**

* Xiao Wang asked Xiao Li's question

*/

Wang.askquestion ("1 + 1 =?");

}

}

By the above example, do you almost understand the callback mechanism, which is an asynchronous callback, let's look at the synchronous callback, the OnClick () method

Now to analyze and analyze the click method of the Android View onclick (); We know that the onclick () is a callback method, and when the user clicks on the view to execute the method, we use the button to give an example.

This is a callback interface for view

/**

* Interface definition for a callback to being invoked when a view is clicked.

*/

Public interface Onclicklistener {

/**

* Called when a view has been clicked.

*

* @param v The view that is clicked.

*/

void OnClick (View v);

}

Package com.example.demoactivity;

Import android.app.Activity;

Import Android.os.Bundle;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.Toast;

/**

* This is equivalent to class A.

* @author xiaanming

* Realized the Onclicklistener interface----> Background one

*/

public class Mainactivity extends Activity implements onclicklistener{

/**

* Class A contains reference to class B-----> Background II

*/

Private button button;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Button = (button) Findviewbyid (R.id.button1);

/**

* Class A calls the method of view, and button extends view-----The >a class calls some method of Class B

*/

Button.setonclicklistener (this);

}

/**

* The callback function called when the user clicks the button, you can do what you want to do

* What I do here is to use toast to cue the onclick

*/

@Override

public void OnClick (View v) {

Toast.maketext (Getapplication (), "OnClick", Toast.length_long). Show ();

}

}

The following is the Setonclicklistener method of the view class, which is equivalent to Class B, and only the key code is posted.

/**

* This view is equivalent to Class B

* @author xiaanming

*

*/

public class View implements Drawable.callback, Keyevent.callback, Accessibilityeventsource {

/**

* Listener used to dispatch click events.

* This field should was made private, so it's hidden from the SDK.

* {@hide}

*/

protected Onclicklistener Monclicklistener;

/**

* Setonclicklistener () parameter is Onclicklistener interface------> Background Three

* Register a callback to being invoked when the this view is clicked. If this view isn't

* clickable, it becomes clickable.

*

* @param l The callback that'll run

*

* @see #setClickable (Boolean)

*/

public void Setonclicklistener (Onclicklistener l) {

if (!isclickable ()) {

Setclickable (TRUE);

}

Monclicklistener = l;

}

/**

* Call this view ' s Onclicklistener, if it is defined.

*

* @return True There was a assigned onclicklistener that is called, false

* Otherwise is returned.

*/

public boolean PerformClick () {

Sendaccessibilityevent (accessibilityevent.type_view_clicked);

if (Monclicklistener! = null) {

Playsoundeffect (Soundeffectconstants.click);

This is not the equivalent of Class B calls a Class A method D, this is the so-called callback method

Monclicklistener.onclick (this);

return true;

}

return false;

}

}

This example is the typical Android callback mechanism, after reading this you do not further understand the callback mechanism? Thread run () is also a callback method, when the start () method that executes the thread will call back to the Run () method, and the processing message is more classic and so on

This is the younger brother to the callback mechanism of a little humble opinion, do not understand please leave a message, if there are errors to point out! Thanks a lot!

Callback Mechanisms in Java

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.