An example of a simple use of Android handler

Source: Internet
Author: User

To open a thread in front and show progress with the progress bar This article, we use the thread to achieve such a simple function, is to click the button, load progress bar. But there is no discovery, click once, click again will not be effective. We need to show the next picture every time we click. Eternal Profit Meeting Casino

There is a need to introduce the Android messaging mechanism, simply, Handler, Looper, and the use of the message queue. Here we are using a simple example to illustrate the use of Handler, which is to send a number 5 to the message queue each time the button is clicked. Or do it in a paintingactivity.

Paintingactivity:

01 privateButton btn_next;
02 privateHandler pic_hdl;
03
04 @Override
05 protectedvoidonCreate(Bundle savedInstanceState) {
06     // TODO Auto-generated method stub
07     super.onCreate(savedInstanceState);
08     setContentView(R.layout.painting);
09     
10     btn_next = (Button)findViewById(R.id.btn_loadnext);
11     btn_next.setOnClickListener(newButtonListener());
12     
13     pic_hdl = newPicHandler();
14 }
15
16 classButtonListener implementsOnClickListener{
17
18     @Override
19     publicvoidonClick(View v) {
20         // TODO Auto-generated method stub
21         if( v.getId() == R.id.btn_loadnext){
22             Message msg = pic_hdl.obtainMessage();
23             msg.what = 4;
24             pic_hdl.sendMessage(msg);
25         }
26     }
27     
28 }
29
30 classPicHandler extends{
31
32     @Override
33     publicvoidhandleMessage(Message msg) {
34         // TODO Auto-generated method stub
35         intwhat = msg.what;
36         System.out.println("what: "+ what);
37     }
38     
39 }

It was so simple that it was done. Demo Effect:

Briefly describe the process:

    1. Private Handler PIC_HDL; Define a Handler, while PIC_HDL = new Pichandler (); is to create a new Handler from our defined class Pichandler;
    2. Then look at the button OnClick event, obtainmessage to get a Msg object, and then assign a value to Msg.what, SendMessage sends the MSG object to the message queue;
    3. Returning to Pichandler, we can easily regain the value of what is in the message queue in Handlemessage. Then print it in Log.

And then we'll go deeper Handler this thing.

An example of a simple use of Android 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.