During Mobile Phone development, some time-consuming operations are often encountered. At this time, the progress bar will start to work in handy.
This demo shows the progressbar progress bar and progressdialog progress box.
1. progressdialog progress box. effect:
CodeAs follows:
1 // Progress dialog box button listener
2 Class Prossbuttonlistener Implements Onclicklistener {
3 @ Override
4 Public Void Onclick (view v ){
5 Mydialog = progressdialog. Show (progressbardemo. This , "Progress bar title ",
6 "Progress bar content ", True );
7
8 New Thread (){
9 Public Void Run (){
10 Try {
11 /* Enter the background to runProgramFragment */
12 /* In order to see the effect clearly, use the pause for 3 seconds as an example. */
13 Sleep (3000 );
14 } Catch (Exception e ){
15 E. printstacktrace ();
16 } Finally {
17 // Unmount the created mydialog object.
18 Mydialog. Dismiss ();
19 }
20 }
21 }. Start (); /* Start running the running thread */
22 }
23 }
2. progress bar dialog box. Two conditions are used to dynamically display the progress bar scale.
1. Handle Method
As follows:
The Code is as follows:
1 // Progress bar handle button monitoring
2 Class Prossbarhandlebuttonlistener Implements Onclicklistener {
3 @ Override
4 Public Void Onclick (view v ){
5 Progressbarhandle. setvisibility (view. Visible );
6 Myprossbarhandletext. setvisibility (view. Visible );
7 Progressbarhandle. setmax (1000 );
8 New Thread (){
9 Public Void Run (){
10 For ( Int I = 0; I <= 1000 ;){
11 Try {
12 /* Write the running progress bar here */
13 Message MSG = New Message ();
14 MSG. What = 1;
15 MSG. getdata (). putint ("size", I );
16 Handler. sendmessage (MSG ); // Handle sends messages
17 /* In order to see the effect clearly, the pause for 1 second is used as an example. */
18 Sleep (100 );
19 I + = 10;
20 } Catch (Exception e ){
21 Handler. obtainmessage (-1). sendtotarget ();
22 E. printstacktrace ();
23 }
24 }
25 }
26 }. Start (); /* Start running the running thread */
27 }
28 }
29
30 // Handle receives messages
31 Private Handler handler = New Handler (){
32
33 @ Override
34 Public Void Handlemessage (Message MSG ){
35 Switch (Msg. What ){
36 Case 1:
37 Progressbarhandle. setprogress (msg. getdata (). getint ("size "));
38 Float Num = ( Float ) Progressbarhandle. getprogress ()/( Float ) Progressbarhandle. getmax ();
39 Int Result = ( Int (Num * 100 );
40 System. Out. println ("progressbarhandle. getprogress () ======" + progressbarhandle. getprogress ());
41 Myprossbarhandletext. settext (result + "% ");
42 If (Progressbarhandle. getprogress () = progressbarhandle. getmax ()){
43 Toast. maketext (progressbardemo. This , "Download succeeded", 1). Show ();
44 Progressbarhandle. setvisibility (view. Gone );
45 Myprossbarhandletext. setvisibility (view. Gone );
46 }
47 Break ;
48
49 Case -1:
50 Toast. maketext (progressbardemo. This , "Download failed", 1). Show ();
51 Break ;
52 }
53 }
54 };
2. Use the asynctask method, which has the same effect as handle.
The Code is as follows:
1 // Progress bar synctask button listening
2 Class Prossbarsyncbuttonlistener Implements Onclicklistener {
3 @ Override
4 Public Void Onclick (view v ){
5 New Asyncloader(cmd.exe cute (void) Null );
6 }
7 }
8
9
10 // Asynctask task execution
11 Class Asyncloader Extends Asynctask <void, integer, integer> {
12 @ Override
13 // Run the command before the doinbackground method is executed.
14 Protected Void Onpreexecute (){
15 Progressbarhandle. setvisibility (view. Visible );
16 Myprossbarhandletext. setvisibility (view. Visible );
17 Progressbarhandle. setmax (100000 );
18 }
19
20 // Perform specific time-consuming operations
21 Protected Integer doinbackground (void... Params ){
22 // It is displayed on a progress bar scale of 10 thousand.
23 Int Totalsize = 100000;
24 For ( Int I = 0; I <100000 ;){
25 Publishprogress (I ); // Transmit data to onprogressupdate by pushing messages for execution
26 I + = 10;
27 }
28 Return Totalsize;
29 }
30 // Execute the doinbackground Method
31 Protected Void Onprogressupdate (integer... progress ){
32 Progressbarhandle. setprogress (Progress [0]);
33 Float Num = ( Float ) Progressbarhandle. getprogress ()/( Float ) Progressbarhandle. getmax ();
34 Int Result = ( Int (Num * 100 );
35 Myprossbarhandletext. settext (result + "% ");
36 }
37
38 // Run the following command after the doinbackground method is complete:
39 Protected Void Onpostexecute (integer result ){
40 Toast. maketext (progressbardemo. This , "Download succeeded, downloaded" + result, 1). Show ();
41 Myprossbarhandletext. setvisibility (view. Gone );
42 Progressbarhandle. setvisibility (view. Gone );
43 }
44 }
Demo: http://files.cnblogs.com/fbsk/proressbardemo.zip