As a developer we often need to stand on the user's point of view, such as when the App store download software, when the user clicks the download button, there will be a download Progress prompt page appears, now we are through the thread hibernation simulation download Progress update demo, (here for the convenience of setting up the dialog progress bar on the screen):
Layout Interface code (deploy only one button:
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"// Real Project recommended uniform definition of text resources in Strings.xml under Res 9 Android:o nclick= "Begin"/> </LinearLayout>
Java Code implementation (progress update via process):
1 Public classProgressbardemoextendsappcompatactivity {2 @Override3 protected voidonCreate (@Nullable Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.progressbar);6 }7 Public voidbegin (View v) {8 //Instantiate progress Bar dialog box (progressdialog)9 FinalProgressDialog PD =NewProgressDialog ( This);TenPd.settitle ("Please wait a moment"); One //Set dialog progress bar style to horizontal A Pd.setprogressstyle (progressdialog.style_horizontal); - //Set Prompt Information -Pd.setmessage ("Trying to download ..."); the //Set dialog progress bar to display at the top of the screen (convenient) - Pd.getwindow (). setgravity (gravity.top); -Pd.setmax (100); -Pd.show ();//call the Show method to display the Progress Bar dialog box + //using anonymous inner classes to implement threads and start - NewThread (NewRunnable () { + intinitial = 0;//Initial Download Progress A @Override at Public voidrun () { - while(Initial<pd.getmax ()) {//set up a loop condition -Pd.setprogress (INITIAL+=40);//Set each completion - Try { -Thread.Sleep (1000); -}Catch(interruptedexception e) { in e.printstacktrace (); - } to } +Pd.dismiss ();//dialog box disappears when progress is complete - } the }). Start (); * } $}
ProgressDialog (Dialog progress bar) by implementing thread Update in Android