进度条与线程

package com.yanjun;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
   private ProgressBar mProgress;
   private int i = 0;
   private Handler mHandler = new Handler();

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mProgress = (ProgressBar) findViewById(R.id.progressBar1);

     new Thread( new Runnable() {

       public void run() {

         while (i < 100) {
           try {
             new Thread().sleep(1000);
          } catch (InterruptedException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
          }
          i = i + 10;
          mHandler.post( new Runnable() {

             public void run() {
              mProgress.setProgress(i);
            }
          });
        }
      }
    }).start();

  }
}

你可能感兴趣的:(进度条与线程)