Explain the two kinds of event handling mechanism of Android _android

Source: Internet
Author: User
Tags event listener static class

UI programming is usually accompanied by event handling, and Android is no exception, and it provides two ways of event handling: callback based event handling and listener based event handling.

For listener-based event handling, the main purpose is to bind specific event listeners to the Android interface component; For callback-based event handling, the main approach is to rewrite the Android component-specific callback function, and most of the Android interface components provide the callback function for the event response, We can rewrite them mainly.

A listener-based event handling

This is a more "object-oriented" approach to event handling than for callback based event processing. In the listener model, there are mainly three types of objects involved:

1 Event Source: The source of an event, usually a variety of components, such as buttons, Windows, and so on.

2 Event Events: Events encapsulate specific information about a specific event that occurs on an interface component, which is typically passed through event events objects if the listener needs to obtain information about the events that occur on the interface component.

3 Incident Listener Event Listener: Responsible for listening to event source events, and to handle different events accordingly.

The listener-based event-handling mechanism is a delegated delegation event handler, which delegates the entire event to the event listener and responds to the event by the listener. This approach separates the event source from the event listener and facilitates the maintainability of the provider.

Example:

The Onlongclicklistener listener in the view class is defined as follows: (do not need to pass events)

Public interface Onlongclicklistener { 

  boolean onlongclick (View v); 

} 

Public interface Onlongclicklistener {
  boolean onlongclick (View v);
}

The Onlongclicklistener listener in the view class is defined as follows: (You need to pass an event motionevent)

Public interface Ontouchlistener { 

  boolean ontouch (View V, motionevent event); 

} 

Public interface Ontouchlistener {
  boolean ontouch (View V, motionevent event);
}

Two callback-based event handling

The event-handling model based on callback is simpler than the listener based event-handling model, in which the event source and event listener are in one, meaning there is no independent event listener. When a user triggers an event on a GUI component, the component's own specific function is responsible for handling the event. Event handling is typically implemented by overriding the event handler functions of the Override component class.

Example:

The view class implements a series of callback functions in the Keyevent.callback interface, so the event-handling mechanism based on the callback is implemented through custom view, which overrides these event-handling methods when customizing the view.

Public interface Callback { 

  //Almost all callback-based event handlers return a Boolean value that is used to 

  //To identify whether the handler can fully handle the event 

//return True. Indicates that the function has fully handled the event and that the event does not propagate out 

//returns FALSE, indicating that the function does not fully handle the event, and that the event propagates out 

   boolean onKeyDown (int keycode, keyevent event); 

   Boolean onkeylongpress (int keycode, keyevent event); 

   Boolean onKeyUp (int keycode, keyevent event); 

   Boolean onkeymultiple (int keycode, int count, keyevent event); 

} 

Public interface Callback {
  //Almost all callback-based event handlers return a Boolean value that is used to
  //To identify whether the handler can fully handle the event
//return True. Indicates that the function has fully handled the event and that the event does not propagate out
//returns FALSE, indicating that the function does not fully handle the event, and that the event propagates out
    boolean onKeyDown (int keycode, keyevent event);
    Boolean onkeylongpress (int keycode, keyevent event);
    Boolean onKeyUp (int keycode, keyevent event);
    Boolean onkeymultiple (int keycode, int count, keyevent event);
}

Third, compared to

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

The Android event-handling mechanism ensures that listener-based event processing takes precedence over callback-based event handling;

In some specific cases, the event handling mechanism based on callback can improve the cohesion of the program better.

Iv. event-handling processes based on custom listeners

In the actual project development, we often need to customize the listener to implement custom business process processing, and generally not based on GUI interface as an event source. This is illustrated by an example of a common app update, which, during the automatic update process, there will be two states: Download and download complete, and our program needs to do different things in these two states, "download" need to be in the UI interface real-time display package download progress, "Download Complete", cancel the display of progress bar. Here is a simulation that focuses on the event handling process of the custom listener.

4.1 Define the event listener as follows:

Public interface downloadlistener{public
  void ondownloading (int porgress);//processing function public void during download
  Ondownload ()//download completed processing function
}

4.2 Implementation of the download operation of the tool class code is as follows:

public class downloadutils{
  private static downloadutils instance=null;
  Private Private () {
  } public
  static synchronized Downloadutils instance () {
   if (instance==null)
   { Instance=new downloadutils ();
   }
   Returns instance
  } 
}
Private Boolean is downloading=ture;
private int progress=0;
The actual development of this function requires a descendant URL as a parameter to obtain the server-side installation package location public

void Download (Downloadlistener listener) throws interruptdeexception{while
(isdownloading) {
     listener.ondownloading (progress);
     Simple simulation of the download process
 thread.sleep (1000);
     progress+=10;
  if (progress>=100) {
  isdownloading=false
 
  }
}}
Download complete
listener.ondownload ();
}

4.3 Finally simulate the event source in the main function:

public class downloadui{public
  static void Main (sting[] args) {
     try{
   downloadutils.instance (). Download ( New Mydownloadlistener ());
 } catch (Interruptedexceptiob e) {
 e.printstacktrace ();
 }
 }
 private static class Mydownloadlistener implements downloadlistener{
 @Override public
void ondownloading (int Progress) {
System.out.println ("Download progress is:" +progress);
}
 @Override public
void ondownloaded () {
System.out.println ("Download Complete")}}

To run the emulator, enter the following:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.