DEMO of the Android ProgressBar progress bar and ProgressDialog progress box

Source: Internet
Author: User

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:

The Code is as follows: Copy codeThe Code is as follows: // button listening in the Progress dialog box
Class ProssButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
MyDialog = ProgressDialog. show (ProgressbarDemo. this, "progress bar title ",
"Progress bar content", true );

New Thread (){
Public void run (){
Try {
/* Write the program fragment to be run in the background */
/* To see the effect clearly, use 3 seconds to suspend for demonstration */
Sleep (3000 );
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
// Uninstall the created myDialog object.
MyDialog. dismiss ();
}
}
}. Start ();/* start running thread */
}
}

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:Copy codeThe Code is as follows: // handle button listening on the progress bar
Class ProssBarHandleButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
ProgressBarHandle. setVisibility (View. VISIBLE );
MyProssBarhandleText. setVisibility (View. VISIBLE );
ProgressBarHandle. setMax (1000 );
New Thread (){
Public void run (){
For (int I = 0; I <= 1000 ;){
Try {
/* Write the running progress bar here */
Message msg = new Message ();
Msg. what = 1;
Msg. getData (). putInt ("size", I );
Handler. sendMessage (msg); // handle sends a message
/* In order to see the effect clearly, suspend for 1 second as the demonstration */
Sleep (100 );
I + = 10;
} Catch (Exception e ){
Handler. obtainMessage (-1). sendToTarget ();
E. printStackTrace ();
}
}
}
}. Start ();/* start running thread */
}
}

// Handle receives messages
Private Handler handler = new Handler (){

@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
ProgressBarHandle. setProgress (msg. getData (). getInt ("size "));
Float num = (float) progressBarHandle. getProgress ()/(float) progressBarHandle. getMax ();
Int result = (int) (num * 100 );
System. out. println ("progressBarHandle. getProgress () ======" + progressBarHandle. getProgress ());
MyProssBarhandleText. setText (result + "% ");
If (progressBarHandle. getProgress () = progressBarHandle. getMax ()){
Toast. makeText (ProgressbarDemo. this, "downloaded", 1). show ();
ProgressBarHandle. setVisibility (View. GONE );
MyProssBarhandleText. setVisibility (View. GONE );
}
Break;

Case-1:
Toast. makeText (ProgressbarDemo. this, "Download failed", 1). show ();
Break;
}
}
};

2. Use the AsyncTask method, which has the same effect as handle.
The Code is as follows:Copy codeThe Code is as follows: // progress bar synctask button listening
Class ProssBarSyncButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
New asyncloader(cmd.exe cute (Void) null );
}
}

// AsyncTask task execution
Class AsyncLoader extends AsyncTask <Void, Integer, Integer> {
@ Override
// Run the command before the doInBackground method is executed.
Protected void onPreExecute (){
ProgressBarHandle. setVisibility (View. VISIBLE );
MyProssBarhandleText. setVisibility (View. VISIBLE );
ProgressBarHandle. setMax (100000 );
}

// Perform specific time-consuming operations
Protected Integer doInBackground (Void... params ){
// It is displayed on a progress bar of 10 thousand.
Int totalSize = 100000;
For (int I = 0; I <100000 ;){
PublishProgress (I); // transmits data to the onProgressUpdate method by pushing messages.
I + = 10;
}
Return totalSize;
}
// Execute the doInBackground Method
Protected void onProgressUpdate (Integer... progress ){
ProgressBarHandle. setProgress (progress [0]);
Float num = (float) progressBarHandle. getProgress ()/(float) progressBarHandle. getMax ();
Int result = (int) (num * 100 );
MyProssBarhandleText. setText (result + "% ");
}

// Execute after the doInBackground method is complete
Protected void onPostExecute (Integer result ){
Toast. makeText (ProgressbarDemo. this, "Download successful, downloaded" + result, 1). show ();
MyProssBarhandleText. setVisibility (View. GONE );
ProgressBarHandle. setVisibility (View. GONE );
}
}

Click to download DEMO

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.