Two Android event processing models

Source: Internet
Author: User

Ui programming is usually accompanied by event processing, and Android is no exception. It provides two methods for event processing: callback-based event processing and listener-based event processing.

For listener-based event processing, it is mainly to bind a specific event listener to the android interface component. For callback-based event processing, the main method is to rewrite the specific callback function of the android component, most Android interface components provide callback functions for event responses. You can simply rewrite them.

Listener-based event processing

Compared with callback-based event processing, this is a more object-oriented event processing method. In the listener model, three types of objects are involved:

1) event Source: the source of the event, usually a variety of components, such as buttons and windows.

2) event: the event encapsulates the details of a specific event on the interface component. If the listener needs to obtain information about the event on the interface component, it is generally passed through the event object.

3) event listener: listens to events generated by the event source and processes different events accordingly.

The listener-based event processing mechanism is a delegated delegation event processing method. The event source delegates the entire event to the event listener, and the listener responds to the event. This processing method separates the event source and event listener, facilitating the maintainability of the provider.

Example:

The onlongclicklistener listener in the View class is defined as follows: (events do not need to be passed)

Public interface onlongclicklistener {<br/> Boolean onlongclick (view V); <br/>}

The onlongclicklistener listener in the View class is defined as follows: (the event motionevent needs to be passed)

Public interface ontouchlistener {<br/> Boolean ontouch (view V, motionevent event); <br/>}

2. Callback-based event processing

Compared with the listener-based event processing model, the callback-based event processing model is simpler. In this model, the event source and event listener are integrated, that is, there is no independent event listener. When a user triggers an event on a GUI component, the specific function of the component processes the event. Generally, event processing is implemented by overwriting the event processing function of the override component class.

Example:

The View class implements a series of callback functions in the keyevent. Callback interface. Therefore, the callback-based event processing mechanism is implemented through a custom view, and these event processing methods can be rewritten during custom views.

Public interface callback {<br/> // almost all callback-based event processing functions return a Boolean value, the return value is used to <br/> // identify whether the handler can fully process the event <br/> // return true, indicating that the Handler has fully handled the event, this event will not be propagated <br/> // false is returned, indicating that the function has not completely handled the event and the event will be propagated <br/> Boolean onkeydown (INT keycode, keyevent event); <br/> Boolean onkeylongpress (INT keycode, keyevent event); <br/> Boolean onkeyup (INT keycode, keyevent event ); <br/> Boolean onkeymultiple (INT keycode, int count, keyevent event); <br/>}

Comparison

The listener-based event model conforms to the single responsibility principle, and the event source and event listener are implemented separately;

Android's event processing mechanism ensures that listener-based event processing takes precedence over callback-based event processing;

Under certain circumstances, the callback-based event processing mechanism will improve the cohesion of the program.

4. Process of event processing based on custom listeners

In actual project development, we often need a custom listener to process custom business processes, and generally it is not based on the GUI interface as the event source. The following describes how to automatically update a common app. During the automatic update process, two statuses exist: Download and download completed, our program needs to do different things in these two States. "Download" needs to display the package download progress in real time on the UI interface. After "Download complete, cancel the display of the progress bar. Here we conduct a simulation, focusing on the event processing process of the custom listener.

4.1) define the event listener as follows:

Public interface downloadlistener {<br/> Public void ondownloading (INT progress); // processing function during download <br/> Public void ondownloaded (); // processed functions downloaded <br/>}

4.2) the tool code for downloading is as follows:

Public class downloadutils {</P> <p> Private Static downloadutils instance = NULL; </P> <p> private downloadutils () {<br/>}</P> <p> Public static synchronized downloadutils instance () {<br/> If (instance = NULL) {<br/> instance = new downloadutils (); <br/>}< br/> return instance; <br/>}</P> <p> private Boolean isdownloading = true; </P> <p> private int progress = 0; </P> <p> // in actual development, this function requires URL as a parameter to obtain the installation package location on the server. <br/> Public void download (downloadlistener listener) throws interruptedexception {<br/> while (isdownloading) {<br/> listener. ondownloading (Progress); <br/> // a simple simulation of the download process <br/> thread. sleep (1000); <br/> progress + = 10; <br/> If (Progress> = 100) {<br/> isdownloading = false; <br/>}< br/> // download completed <br/> listener. ondownloaded (); <br/>}</P> <p>}

4.3) Finally, simulate the event source in the main function:

Public class downloadui {</P> <p> Public static void main (string [] ARGs) {<br/> try {<br/> downloadutils. instance (). download (New mydownloadlistener (); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Private Static class mydownloadlistener implements downloadlistener {</P> <p> @ override <br/> Public void ondownloading (INT progress) {<br/> system. out. println ("download progress:" + progress); <br/>}</P> <p> @ override <br/> Public void ondownloaded () {<br/> system. out. println ("downloaded"); <br/>}</P> <p>}

Run the above simulation program and the output is as follows:

5 References

1) crazy android handout

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.