Android multithreading progress bar

Source: Internet
Author: User

Update the android progress bar using multiple threads.

Instance tutorial. For details, I have commented out the android multithreading progress bar 01 package com. shougao. hello; 0203 import android. app. activity; 04 import android. OS. bundle; 05 import android. OS. handler; 06 import android. OS. message; 07 import android. view. view; 08 import android. view. view. onClickListener; 09 import android. widget. button; 10 import android. widget. progressBar; 1112 public class HelloActivity extends Activity {13/** Called when the activity is first cr Eated. */1415 private ProgressBar progressBar; 16 private Button startButton; 1718 @ Override19 public void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. main); 2223 progressBar = (ProgressBar) findViewById (R. id. progressbar); 24 startButton = (Button) findViewById (R. id. startButton); 2526 startButton. setOnClickListener (new ProgressBarOnClickListener (); 27} 2829 class ProgressBarOnClickListener implements OnClickListener {30 public void onClick (View v) {31 // set the progress bar to the visible status 32 progressBar. setVisibility (View. VISIBLE); 33 // Add updataThread to message queue 34 updateBarHandler. post (updateThread); 35} 36} 3738 // use an anonymous internal class to re-write the handlerMessage () method 39 in the Handler. // This handler needs to be executed repeatedly, until there is no Message in the Message queue. 40 // The progress bar is refreshed during execution. 41 Handler updateBarHandler = new Handler () {42 @ Override43 public void handleMessage (Message msg) {44 System. out. println ("---------- start handle"); 45 progressBar. setProgress (msg. arg1); 46 updateBarHandler. post (updateThread); // put the thread to be executed into the queue 47} 48}; 4950 // Thread class, this class declares using an anonymous internal class 51 Runnable updateThread = new Runnable () {52 int I = 0; 5354 public void run () {55 // TODO Auto-generated method stub56 Sy Stem. out. println ("---------- Begin Thread"); 57 I + = 20; 58 // get a Message object. The Message class is 59 Message msg = updateBarHandler provided by android. obtainMessage (); 60 // set the value of the arg1 parameter of the Message object to i61 msg. arg1 = I; // use the two member variables arg1 and arg2 to transmit messages. The advantage is that the system performance consumption is less than 62 try {63 Thread. sleep (3000); // sleep the current thread for 1000 milliseconds 64} catch (InterruptedException ex) {65 ex. printStackTrace (); 66} 67 // Add the Message object to the message Queue. When the next logoff calls the Message queue, use this message to display the progress bar 68 // Or put the progress Message with an increase of 10% in the queue of updateBarHandler. 69 if (I <101) {70 updateBarHandler. sendMessage (msg); 71} 72 // if the I value is 10073 if (I = 100) {74 // remove 75 updateBarHandler from the queue. removeCallbacks (updateThread); 76} 77} 78}; 79}

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.