The use of Android--handler

Source: Internet
Author: User
Original link: The use of http://byandby.javaeye.com/blog/832467 http://byandby.javaeye.com/blog/832915 handler (i)

Handler Basic Concepts:
Handler is mainly used for the processing of asynchronous messages: When a message is sent, it is first entered into a message queue, the function that sends the message is returned immediately, and the other part takes the message out of the message queue one at a time, and then the message is sent out, and the message is not synchronized.
This mechanism is often used to handle relatively lengthy operations.

Use an example to simply introduce handler.
Example 1: One application has 2 buttons (start, end), and when the Start button is clicked, executes a thread that outputs a string of strings at the console and executes the thread every 3 seconds until it is clicked.

Development steps:
1. Create a new Android app
2. Add 2 Button control labels to the layout file and set properties and values for them
3. In the activity, declare the control variable and get the control object based on the ID
4. In the activity, create a handler object
5. In the activity, create a Runnable object
(a) In the form of anonymous internal classes
b The action to be performed is written in the run () method in the Runnable object
I print a word.
Ii. invoke the Postdelayed () method of the Runnable object
6, in the activity, write the Start button required listeners, and binding
(a) In this listener's onclick () method, call the handler post () method and place the thread object to be executed in the queue.
7, in the activity, write the end button to need the listener, and to help set
(a) In this listener's onclick () method, call the handler Removecallbacks () method to delete the thread object that is not executing in the queue.
b
The following is the code for the activity:
Java code     package android.handler;       import android.app.activity;    import android.os.bundle;    import android.os.handler;    import android.view.view;    import android.view.view.onclicklistener;    import android.widget.button;       public class handlertest extends activity {        /** called when the activity is first created. */        private Button startButton;        private Button endButton;                 @Override         public void oncreate (bundle savedinstancestate)  {            super.oncreate (SavedinstancEstate);     &NB

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.