Handler event handling mechanism and message passing mechanism

First, based on event handling mechanism listening

  1. Based on listening time handling mechanism model:

Event sensing mechanism in the process flow by the event source, event, event listener objects categories as follows:

Step 1: Set a listener for an event source (assembly), for monitoring a user operation

Step 2: user's actions, triggered the listener event sources

Step 3: generating the corresponding event object

Step 4: The event source object as a parameter to event listeners

step 5: Event event listener objects determined, executing the corresponding event handler (processing method corresponding to the event)

  1. Five different forms of use

1) direct use of anonymous inner classes

Usually the most commonly used: directly after setXxxListener, the method can be rewritten inside; usually a temporary use, reusability is not high!

2) inner classes

And above anonymous inner classes different Oh! Use advantages: can be multiplexed in the class, interface components can be direct access to all of the outer class!

3) using an external class

Also create a Java file handling events of this form with less! Because the external class can not directly access the components of the user interface class, through constructor will use the incoming assembly; result of this is the code simple enough!

4) direct use as an event listener Activity

Let XxxListener Activity class implements an event listener interface that defines override the event handler method corresponding eg in an Activity: Actitity achieved OnClickListener interface rewrote the onClick (view) method when you add the event listener object is a certain form, directly setXxx.Listener (this) to

5) bind directly to the label

Directly corresponds xml layout file to obtain Activity define an event handler method eg: public void myClick (View source) source corresponding to the event source (assembly), then the layout file corresponding to the formation to be a triggering event, provided a property: onclick = " myclick "to

        

Second, callback-based event handling mechanism

When we do not need to bind event listeners in the Java file click event can be completed callback that will handle components corresponding event, that event is handled by the event source (component) itself!

  1. Callback method

1) The definition and functions as a means of functionally separated a coupling design solutions;

2) In Java callback is accomplished through the interface, as a system architecture must have its own operating environment, and the need to provide users with implementation of the interface;

3) implementation depends on the client, so that you can achieve a unified interface to achieve different;

4) systems "callback" in different states our implementation class, so as to achieve separation of interface from implementation!

  1. Two usage scenarios

1) Custom View (assembly)

When a user stimulate an event on the GUI component, the component has its own particular method will be responsible for handling the event.

Common usage: Inheriting the basic GUI components, event handling method to rewrite the component that custom View.

 

Common View callback method:

① trigger event on the screen components: boolean onTouchEvent (MotionEvent event);

② When a button is pressed on the assembly: boolean onKeyDown (int keyCode, KeyEvent event);

③ When a release button on the components: boolean onKeyUp (int keyCode, KeyEvent event);

④ long press a button assembly: boolean onKeyLongPress (int keyCode, KeyEvent event);

⑤ keyboard shortcuts events :: boolean onKeyShortcut (int keyCode, KeyEvent event);

⑥ trackball screen trigger event on the component: boolean onTrackballEvent (MotionEvent event);

⑦ When the focus changes the component, and in front of the six different, this method can only be rewritten in View Oh! protected void onFocusChanged (boolean gainFocus, int direction, Rect previously FocusedRect)

 

Note: Use in a custom xml view layout, the need to use "Fully qualified class name of"

2) the spread of callback-based event

View the callback method return values ​​are boolean type, the return value is used to identify whether this method has been completely processed the event. If false, it indicates not processed, then spread out, the callback method Activity related to the starting components are located; is true it will not spread.

Spread sequence of events: listeners ---> callback method view component ---> Activity callback method

                  

Three, Handler message passing mechanism

Handler of use:

  1. Handler wrote in the main thread

In the main thread, because the system has initiated a Looper object, so we created directly Handler object, it can transmit and process information.

  1. Handler written in the child thread

If Handler wrote in a child thread, then we need to create an object of a Looper. Process created as follows:

         1) direct call Looper.prepare () method to create Looper object for the current thread, and its construction will create matching MessageQueue

2) Create a Handler object, overwriting handleMessage () method can process information from the other thread

3) call Looper.loop () method to start Looper

Guess you like

Origin www.cnblogs.com/pomodoro/p/11468998.html