请查看以下示例代码。Toast消息被显示,但进度对话框从未隐藏。为什么?
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
public class LoadExamActivity extends Activity implements Runnable{
ProgressDialog pd;
Handler Finished = new Handler(){
@Override
public void handleMessage(Message msg){
Toast.makeText(getApplicationContext(), "DONE!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exam);
Toast.makeText(this, "START!", Toast.LENGTH_SHORT).show();
pd = new ProgressDialog(this);
pd.show(this, "Waiting...", "Please wait five seconds...");
Thread th = new Thread(this);
th.start();
}
public void run() {
//To change body of implemented methods use File | Settings | File Templates.
for (int i = 0; i < 5; i++)
{
try
{
Thread.sleep(1000);
}catch(Exception e){}
}
Finished.sendEmptyMessage(0);
}
}
五秒钟后,“DONE”消息将显示,但进度对话框不会关闭,即使我在pd.show()后面立即放置pd.dismiss()也无法关闭进度对话框,我不知道为什么会发生这种情况,这让我很疯狂!