android 标题进度条/ProgressDialog ---自己的一个例子

为了更好的提醒用户后台程序的进度,推荐使用这个

关键代码:

public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
             requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//给Activity注册界面进度条功能
             setContentView(R.layout.main);
             setProgressBarIndeterminateVisibility(true);//显示进度条
             setProgressBarIndeterminateVisibility(false);//不显示进度条}



下面是我的一个实际的例子:

标题栏中的进度条:


ProgressDialog:

完整代码如下:
package gyf.google; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; public class g3 extends Activity { /** Called when the activity is first created. */ static final int PROGRESS_DIALOG = 0; Button button; ProgressThread progressThread; ProgressThread2 pThread2; ProgressDialog progressDialog; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ///************************************************************ requestWindowFeature(Window.FEATURE_PROGRESS);//请求一个窗口进度条特性风格 setProgressBarVisibility(true); setProgress(0);//初始进度 pThread2=new ProgressThread2(handler2); pThread2.start(); ///**************************************************************** setContentView(R.layout.main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { showDialog(PROGRESS_DIALOG); } }); } protected Dialog onCreateDialog(int id) { switch(id) { case PROGRESS_DIALOG: progressDialog = new ProgressDialog(g3.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("Loading..."); progressThread = new ProgressThread(handler); progressDialog.setMax(100); progressThread.start(); return progressDialog; default: return null; } } // Define the Handler that receives messages from the thread and update the progress final Handler handler = new Handler() { public void handleMessage(Message msg) { int total = msg.getData().getInt("total"); progressDialog.setProgress(total); if (total >= 100){ dismissDialog(PROGRESS_DIALOG); progressThread.setState(ProgressThread.STATE_DONE); } } }; final Handler handler2=new Handler(){ public void handleMessage(Message msg){ int currentNum=msg.getData().getInt("currentNum"); setProgress(currentNum*10); if (currentNum>1000) { pThread2.setState(ProgressThread2.DONE); } } }; private class ProgressThread2 extends Thread{ Handler myHandler; int currentNum; final static int DONE=0; final static int RUNNING=1; int state; ProgressThread2(Handler handler){ myHandler=handler; } public void run(){ state=RUNNING; currentNum=0; while (state==RUNNING) { try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } Message message=myHandler.obtainMessage(); Bundle bundle=new Bundle(); bundle.putInt("currentNum", currentNum); message.setData(bundle); myHandler.sendMessage(message); currentNum++; } } public void setState(int i){ state=i; } } /** Nested class that performs progress calculations (counting) */ private class ProgressThread extends Thread { Handler mHandler; final static int STATE_DONE = 0; final static int STATE_RUNNING = 1; int mState; int total; ProgressThread(Handler h) { mHandler = h; } public void run() { mState = STATE_RUNNING; total = 0; while (mState == STATE_RUNNING) { try { Thread.sleep(100); } catch (InterruptedException e) { Log.e("ERROR", "Thread Interrupted"); } Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("total", total); msg.setData(b); mHandler.sendMessage(msg); total++; } } /* sets the current state for the thread, * used to stop the thread */ public void setState(int state) { mState = state; } } }

你可能感兴趣的:(thread,android,exception,Class,dialog,button)