In Android, ProgressDialog (dialog progress bar) and progressdialog are updated by implementing threads.

Source: Internet
Author: User

In Android, ProgressDialog (dialog progress bar) and progressdialog are updated by implementing threads.

As developers, we often need to consider issues from the user's perspective. For example, when a user clicks the Download button when downloading software in an application mall, the download progress prompt page appears, now we use the thread sleep mode to simulate the download progress update demonstration (here to facilitate setting the dialog progress bar at the top of the screen ):

Layout interface code (only one button is deployed ):

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <Button 6 android: layout_width = "wrap_content" 7 android: layout_height = "wrap_content" 8 android: text = "Download" // for real projects, we recommend that you define the strings configured under res. xml 9 android: onClick = "begin"/> 10 </LinearLayout>

 

JavaCode Implementation(Simulate download progress updates through threads):

1 public class ProgressBarDemo extends AppCompatActivity {2 @ Override 3 protected void onCreate (@ Nullable Bundle savedInstanceState) {4 super. onCreate (savedInstanceState); 5 setContentView (R. layout. progressbar); 6} 7 public void begin (View v) {8 // instantiate progress bar dialog box (ProgressDialog) 9 final ProgressDialog pd = new ProgressDialog (this); 10 pd. setTitle ("Please wait"); 11 // set the dialog progress bar style to level 12 pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); 13 // sets the prompt information 14 pd. setMessage ("Desperately downloading ...... "); 15 // set the dialog progress bar to display at the top of the screen (convenient) 16 pd. getWindow (). setGravity (Gravity. TOP); 17 pd. setMax (1, 100); 18 pd. show (); // call the show method to display the progress bar Dialog Box 19 // use an anonymous internal class to implement the Thread and start 20 new Thread (new Runnable () {21 int initial = 0; // initial download progress 22 @ Override23 public void run () {24 while (initial <pd. getMax () {// set the cyclic condition 25 pd. setProgress (initial + = 40); // set 4026 try {27 Thread. sleep (1000); 28} catch (InterruptedException e) {29 e. printStackTrace (); 30} 31} 32 pd. dismiss (); // when the progress is complete, the dialog box disappears 33} 34 }). start (); 35} 36}

 

Related Article

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.