public class ProgressDialogTest extends Activity{ private static final int MAX_NUMBER=100; private int currentProgress=0; Button button; ProgressDialog progressDialog; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.progress); button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { showDialog(1); currentProgress=0; progressDialog.setProgress(0); handler.sendEmptyMessage(0); } }); handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); if (currentProgress>=MAX_NUMBER) { progressDialog.dismiss(); }else { currentProgress++; progressDialog.incrementProgressBy(1); handler.sendEmptyMessageDelayed(0, MAX_NUMBER); } } }; } @Override protected Dialog onCreateDialog(int id) { progressDialog=new ProgressDialog(ProgressDialogTest.this); progressDialog.setIcon(R.drawable.icon); progressDialog.setMax(MAX_NUMBER); progressDialog.setTitle("ProgressDialog"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton("OK", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); return progressDialog; } }
在AndroidManifest加上android:configChanges="locale|keyboardHidden|orientation",就不会出现横竖屏的时候进度条终止的情况。