Package com. Shao. Android;
Import java. util. Random;
Import Android. App. activity;
Import Android. App. progressdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public class main extends activity implements onclicklistener {
/** Called when the activity is first created .*/
Private Static final int max_progress = 100;
Private progressdialog;
Private handler progresshandler;
Private int progress;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button button1 = (button) findviewbyid (R. Id. btn1 );
Button button2 = (button) findviewbyid (R. Id. btn2 );
Button1.setonclicklistener (this );
Button2.setonclicklistener (this );
}
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Switch (V. GETID ())
{
Case R. Id. btn1:
Showprogressdialog (progressdialog. style_horizontal );
Break;
Case R. Id. btn2:
Showprogressdialog (progressdialog. style_spinner );
Break;
}
}
// Display the progress dialog box. style indicates the style of the Progress dialog box.
Private void showprogressdialog (INT style)
{
Progressdialog = new progressdialog (this );
Progressdialog. seticon (R. drawable. Wait );
Progressdialog. settitle ("processing data ...");
Progressdialog. setmessage ("Please wait ...");
// Set the style of the Progress dialog box
Progressdialog. setprogressstyle (style );
// Set the maximum progress value in the Progress dialog box.
Progressdialog. setmax (max_progress );
// Set the pause button in the Progress dialog box
Progressdialog. setbutton ("pause", new dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int whichbutton ){
// Todo auto-generated method stub
// Stop the timer execution by deleting the message code
Progresshandler. removemessages (1 );
// Initial recovery progress Value
Progress = 0;
Progressdialog. setprogress (0 );
}
});
Progressdialog. Show ();
Progresshandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
// Todo auto-generated method stub
Super. handlemessage (MSG );
// If (progressdialog. getprogress ()> = max_progress)
If (Progress> = max_progress)
{
// The Progress reaches the maximum value. Close the dialog box.
Progress = 0;
Progressdialog. Dismiss ();
}
Else {
Progress ++;
// Increase the progress by 1
Progressdialog. incrementprogressby (1 );
// Set the time for the next increment (call the handlemessage method) at random
Progresshandler. sendemptymessagedelayed (1,50 + new random (). nextint (500 ));
}
}
};
// Set the initial progress Value
Progress = (Progress> 0 )? Progress: 0;
Progressdialog. setprogress (Progress );
Progresshandler. sendemptymessage (1 );
}
}