Android 程序開發一定會設計網絡數據下載,不免出現等待時間,爲了緩和用戶的等待時間的“煩躁”,一般會使用進度對話框提升用戶正在進行數據下載,請稍候云云~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/progressBtn" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="HelloProgressBar" />
</LinearLayout>
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.widget.Button;
public class HelloAndroid extends Activity {
private static final int PROGRESS_DIALOG = 0;
private ProgressDialog mProgressDlg = null;
private ProgressThread mProgressThd = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup the button that starts the progress dialog
Button btnProgress = (Button) findViewById(R.id.progressBtn);
btnProgress.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(PROGRESS_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG:
mProgressDlg = new ProgressDialog(this);
mProgressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDlg.setTitle("Loading...");
mProgressDlg.setMessage("Please wait for a while...");
mProgressThd = new ProgressThread(handler);
mProgressThd.start();
return mProgressDlg;
default:
return null;
}
}
// Define the Handler that receives messages from the thread and update the
// progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
mProgressDlg.setProgress(total);
if (total >= 100) {
dismissDialog(PROGRESS_DIALOG);
mProgressThd.setState(ProgressThread.STATE_DONE);
}
}
};
/** Nested class that performs progress calculations (counting) */
private class ProgressThread extends Thread {
Handler mHandler = null;
private final static int STATE_DONE = 0;
private final static int STATE_RUNNING = 1;
private int mState = 0;
ProgressThread(Handler h) {
this.mHandler = h;
}
public void run() {
setState(STATE_RUNNING);
int total = 0;
while (STATE_RUNNING == mState) {
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;
}
}
}