公共类 TestActivity 扩展 Activity {
ProgressDialog progressDialog;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
context = this;
showProgress("Data loading...");
new Thread() {
public void run() {
try {
boolean sucess = loadData();
if (sucess) {
Message alertMessage = new Message();
alertMessage.what = 1;
handle.sendMessage(alertMessage);
} else {
Message alertMessage = new Message();
alertMessage.what = 2;
handle.sendMessage(alertMessage);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}.start();
}
private boolean loadData() {
// load data here
return true;
}
private void showProgress(String msg) {
progressDialog = ProgressDialog
.show(TestActivity.this, null, msg, true);
}
private void hideProgress() {
if (progressDialog != null)
progressDialog.dismiss();
}
Handler handle = new Handler() {
public void handleMessage(android.os.Message msg) {
hideProgress();
if (msg.what == 1) {
Toast.makeText(context, "Data Loaded Sucessfully",
Toast.LENGTH_SHORT).show();
} else if (msg.what == 2) {
Toast.makeText(context, "Data Loading failed ",
Toast.LENGTH_SHORT).show();
}
super.handleMessage(msg);
};
};
}