In Android, the sub-thread uses the components in the main thread to solve the problem. The android thread

Source: Internet
Author: User

In Android, the sub-thread uses the components in the main thread to solve the problem. The android thread

In Android, components in the main thread cannot be called by the quilt thread; otherwise, an exception occurs.

The method used here is to useHandler classInCallback (), In the receiving threadMessage classSend the message, and then submit the function to be executed in the threadHandler class. In this way, the thread problems are solved.

The function of the following example is to click an image. The image transparency changes to 50,300 milliseconds and then becomes opaque. The Code is as follows:

 

Public class Demo extends Activity {private ImageView changeImg = null; // ImageView component Handler handler = new Handler (new Handler. callback () {@ Override public boolean handleMessage (Message msg) {if (msg. what = 0) {*** // ID of the Message *** changeImg. setImageAlpha (255); ** // set the image transparency to 255, that is, opacity **} return false ;}); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanc EState); setContentView (R. layout. activity_main); ** changeImg = (ImageView) findViewById (R. id. change_img); ** // bind the image component} private class OnClick implements View. onClickListener {// inherited click listening class @ Override public void onClick (View v) {changeImg. setImageAlpha (50); // sets the changeImg transparency Toast. makeText (MainActivity. this, "this function has not yet been developed. Please wait! ", Toast. LENGTH_SHORT ). show (); new Thread () {// The status of changeImg is resumed with a delay. public void run () {try {sleep (300 ); // Message msg = new Message (); // declare and instantiate a Message class to notify Handler to execute the function msg. what = 0; // ID of the msg Object handler. sendMessage (msg); // send the msg object. This will execute the Callbak () event} catch (InterruptedException e) {// throw an exception e. printStackTrace ();}}}. start ();}}

 

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.