Android Learning note 25--event handling Handler

Source: Internet
Author: User

Handler message delivery mechanism

= = "

The Android messaging mechanism is another form of "event handling", which is primarily designed to address the multithreading problems of Android applications.

The--android platform does not allow the newly-started thread to access the interface component in the activity, which causes the newly-started thread to dynamically change the value of the interface component.

However, in the development of real Android applications, especially in the development of animation games, it is necessary to let the newly-launched thread change the value of the interface component periodically, it needs to be handled by the handler message passing mechanism.

The main functions of the handler class are:

1. Send the message in the newly started thread;

2. Get and process messages in the main thread;

Attention:

In order for the main thread to "timely" process the message sent by the newly-started thread, it can only be implemented by a callback--only the method of handling the message in the handler class needs to be rewritten, and when the newly started thread sends the message,

The method that processes the message in the handler class is automatically recalled.

The handler class contains the following methods for sending and processing messages:

void Handlemessage (Message msg) The method that handles the message, which is typically used to override the
Final Boolean hasmessage (int what) Checks whether Message Queuing contains a message with the What property is the specified value
Final Boolean hasmessage (int what,object obj) Checks whether the message queue contains a message with a value of what property is specified and the Obj object is a message for the specified object
Multiple overloaded message obtainmessage (int what) Get message
Sendemptymessage (int what) Send an empty message
Final Boolean sendemptymessagedelayed (int what,long delaymillis) Specify the number of milliseconds after which an empty message is sent
Final Boolean sendMessage (Message msg) Send Message Now
Final Boolean sendmessagedelayed (Message msg,long delaymillis) Specify the number of milliseconds after which the message is sent

Instance:

Layout file = = "<linearlayout xmlns:android=" http://schemas.android.com/apk/res/android "xmlns:tools="/http Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:gravi ty= "center" android:orientation= "horizontal" tools:context= ". Mainactivity "> <imageview android:id=" @+id/imgvtest "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content"/></linearlayout> code implementation = = "Package Com.example.myhandler;import Java.util.timer;import Java.util.timertask;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.app.activity;import Android.view.menu;import Android.widget.ImageView;public Class Mainactivity extends activity{int[] Images = new int[] {r.drawable.one, r.drawable.two, R.drawable.three, r.drawabl E.four,r.drawable.five, r.drawable.six};int currentimageid = 0; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {Super.oncreate (savedInstancestate); Setcontentview (R.layout.activity_main); final ImageView img = (ImageView) This.findviewbyid ( R.id.imgvtest), final Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {if (msg.what = = 1) {i Mg.setimageresource (images[currentimageid++]); if (Currentimageid >= 6) {Currentimageid = 0;}} Super.handlemessage (msg);}};/ /define timers that allow timers to perform periodic task new timer (). Schedule (new TimerTask () {@Overridepublic void run () {message msg = new Message (); Msg.what = 1;handler.sendmessage (msg);}}, 0, 1000);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is pre Sent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Achieve the effect: 6 images for dynamic toggle display effect

.....

Android Learning note 25--event handling Handler

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.