android--Threading Thread Two implementation Method!

Source: Internet
Author: User

Original address: http://blog.csdn.net/boyupeng/article/details/6208072

There are three points in this article that need to be explained in advance,

One:

There are two ways to implement threading thread in Android:

One is that the extended Java.lang.Thread class
Another is to implement the Runnable interface

Two:
The thread class represents a threading class, and its two most important methods are:
Run ()--contains code executed by the thread at runtime
Start ()--for starting the thread

Three:

The Handler mechanism, which is a bridge between the runnable and activity interactions, sends a message in the Run method, and in Handler, performs different tasks with different messages.

(handler is designed to actually communicate with other threads in the application and the main thread, because only the main thread can update the UI, not the other threads)

Here are two ways to implement the thread, one of which is to extend the Java.lang.Thread class, that is, to write the run () method to the thread:

1  Packagecom.my; 2 Importandroid.app.Activity; 3 ImportAndroid.os.Bundle; 4 ImportAndroid.os.Handler; 5 ImportAndroid.os.Message; 6 ImportAndroid.util.Log; 7 ImportAndroid.view.View; 8 ImportAndroid.view.View.OnClickListener; 9 ImportAndroid.widget.Button; Ten  Public classDemo_for_copyextendsActivity One {   A      Publicbutton button;  -        -      PublicHandler mhandler=NewHandler () the     {   -          Public voidhandlemessage (Message msg) -         {   -             Switch(msg.what) +             {   -              Case1:   + Button.settext (R.STRING.TEXT2);  A                  Break;  at             default:   -                  Break;  -             }   -             Super. Handlemessage (msg);  -         }   -     };  in        -     /**Called when the activity is first created.*/   to @Override +      Public voidonCreate (Bundle savedinstancestate) -     {   the         Super. OnCreate (savedinstancestate);  * Setcontentview (R.layout.main);  $button=(Button) Findviewbyid (R.id.button); Panax Notoginseng    -Thread thread=NewThread (NewRunnable () the         {   + @Override A              Public voidRun () the             {   +LOG.E ("1111", "111111111");  -                 //TODO auto-generated Method Stub $Message message=NewMessage ();  $Message.what=1;  - mhandler.sendmessage (message);  -             }   the         });  - Thread.Start (); Wuyi     }   the}

Secondly, implement the Runnable interface, let the class implement the Runnable interface, and then put the Run method separately:

1  Packagecom.my; 2 Importandroid.app.Activity; 3 ImportAndroid.os.Bundle; 4 ImportAndroid.os.Handler; 5 ImportAndroid.os.Message; 6 ImportAndroid.util.Log; 7 ImportAndroid.view.View; 8 ImportAndroid.view.View.OnClickListener; 9 ImportAndroid.widget.Button; Ten Importandroid.widget.LinearLayout;  One  Public classTitle_change_demoextendsActivityImplementsRunnable A {   -      Publicbutton button;  -      PublicLinearLayout my_layout;  the        -      PublicHandler mhandler=NewHandler () -     {   -          Public voidhandlemessage (Message msg) +         {   -             Switch(msg.what) +             {   A              Case1:   at Button.settext (R.STRING.TEXT2);  -                  Break;  -             default:   -                  Break;  -             }   - my_layout.invalidate ();  in             Super. Handlemessage (msg);  -         }   to     };  +        -     /**Called when the activity is first created.*/   the @Override *      Public voidonCreate (Bundle savedinstancestate) $     {  Panax Notoginseng         Super. OnCreate (savedinstancestate);  - Setcontentview (R.layout.main);  the            +button=(Button) Findviewbyid (R.id.button);  Amy_layout=(LinearLayout) Findviewbyid (r.id.my_layout);  the            +Thread thread=NewThread ( This);  - Thread.Start ();  $     }   $        - @Override -      Public voidRun () the     {   -LOG.E ("OK", "111111111"); Wuyi         //TODO auto-generated Method Stub theMessage message=NewMessage ();  -Message.what=1;  Wu mhandler.sendmessage (message);  -     }   About }   $   

android--Threading Thread Two implementation Method! Go

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.