Android asynchronous callback

Source: Internet
Author: User

Android event processing:
Listener-based and callback-based.
A listener is used to bind a specific event listener to the android interface component. It mainly handles some specific events.
Based on callback, the android component-specific callback method or the activity callback method are rewritten. Android provides Event Response callback methods for most interface components. It can be used to process some universal events.

For the listener-based event processing model, the event source and event listener are separated. When a specific event occurs on the event source, the event is handed over to the event listener for processing, for callback-based event processing models, the event sources and Event Listeners are unified. When a specific event occurs in a time source, the event source is responsible for processing the event.
For example, boolean onKeyMultiple (int keyCode, int repeateCount, KeyEvent event) is used to respond to repeated clicks by pressing the key. Official API spending, which always returns false without handle, must be rewritten for implementation.

The delegation mechanism requires event sources, events, and listeners. The callback mechanism is hard to understand. The popular saying on the internet is: Class A holds Class B references, Class A calls method C of Class B, and Class B calls method D of Class A in turn, D is called the callback method. In fact, D is the method in the callback interface.

Based on my understanding of this idea, I wrote a simple example to help you understand it easily.

A has A plan that needs to be completed by B. B has other work to do. This planning scheme can be done only after it is done. B. After planning the solution, call method A and hand over the solution to.

Class A: implement the callback interface and call A method of Class B.

Public class A implements CallBack {/*** class A holds A reference to class B */private B B; public A (B B) {this. B = B;}/*** Method for executing asynchronous tasks * @ param ask */public void ask (final String ask) {// simulate asynchronous tasks, two tasks, new Thread (new Runnable () {@ Overridepublic void run () {// 1, call class B method at the same time to obtain solution B. excuteMessage (. this, ask); // 2 execute other tasks ();}}). start ();}/*** other tasks */public void task () {System. out. println ("---------");}/*** get solution * callback Method */@ Overridepublic void sovle (String result) {System. out. println (result );}}

Class B: The called method must contain A callback interface parameter and call A method (callback method) of Class)

Public class B {/*** interface parameter method * @ param cb CallBack interface * @ param ask problem */public void excuteMessage (CallBack cb, String ask) {System. out. println ("problem:" + ask); // after a period of time, the scheme has produced String result = "solution 1 "; // Class B calls the method cb in Class A in turn. sovle (result );}}

Callback interface:

/*** CallBack interface ** @ author DB **/public interface CallBack {/*** CallBack method ** @ param result */public void sovle (String result );}

Main:

Public class Test {public static void main (String [] args) {B B B = new B (); // class B registration callback method A a A = new A (B ); // Class A implements the callback interface // Class A references and calls its own method. ask ("ask ");}}


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.