Simple use of Thread, Handler, and ProgressBar (2)

Source: Internet
Author: User

Use Thread and Handler to control the progress of ProgressBar
Public class HandlerProgressBarAty extends Activity {

 

Private int I = 0;

Private ProgressBar mProgressBar;

 

Private android. OS. Handler mHandler = new Handler (){

@ Override

Public void handleMessage (Message msg ){

MHandler. postDelayed (mThread, 1000 );

}

};

 

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. pb );

MProgressBar = (ProgressBar) findViewById (R. id. pb );

Button btn_download = (Button) findViewById (R. id. btn_download );

Btn_download.setOnClickListener (new View. OnClickListener (){

 

@ Override

Public void onClick (View v ){

MProgressBar. setVisibility (View. VISIBLE );

MHandler. post (mThread );

}

});

}

 

Private Thread mThread = new Thread (new Runnable (){


@ Override

Public void run (){

I = I + 10;

MProgressBar. setProgress (I );

Message msg = mHandler. obtainMessage ();

Msg. arg1 = I;

MHandler. sendMessage (msg );

When (I> 100 ){

MHandler. removeCallbacks (mThread );

}

}

 

});

}
Refer to the simple use of Thread and Handler (1)

Author: "Wei Wang's blog"
 

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.