为什么对话框不能与线程并行工作?使用此代码,活动冻结和进度对话框不显示...我需要在下载文件期间显示进度对话框...
在 onCreate 中:
pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.setTitle(null);
pDialog.setMessage(getString(R.string.loading));
在下载方法中:
startReader = true;
pDialog.show();
new Thread(new Runnable(){
public void run(){
for(int i = 1; i <= Integer.parseInt(pages); i++){
try{
if(!isCached(code,i)){
try{
CODE TO DOWNLOAD THE FILE;
Log.d(TAG, "File downloaded: /"+ code + "/" + "pg" + i + ".rsc");
}catch(IOException e){
runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), getString(R.string.reader_errinternetcon), Toast.LENGTH_SHORT).show();
}
});
}
}
}catch(Exception e){
runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
startReader = false;
break;
}
}
if(startReader){
runOnUiThread(new Runnable(){
public void run(){
Intent intent = new Intent(MainActivity.this, ReaderActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("Pages", pages);
intent.putExtra("Code", code);
getApplicationContext().startActivity(intent);
}
});
}
}
}).start();
pDialog.dismiss();