我按照给我的指示(1. ASyncTask):
Download a file with Android, and showing the progress in a ProgressDialog
它起到了一定的作用。将显示文件下载(使用我自己的下载代码)和旋转进度对话框。大。唯一的问题是代码完成时对话框没有关闭。那可能是因为我无法使用PreExecute工作。我不太确定在哪里放。代码运行的任何地方都会引发许多错误。让我发布我目前的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double getWidth = size.x * 0.9;
scWidth = (int)getWidth;
cWhite = getResources().getColor(R.color.white);
cBlack = getResources().getColor(R.color.black);
cTrans = getResources().getColor(R.color.transparent);
cBlue = getResources().getColor(R.color.blue);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
version = sharedPreferences.getString("version", "0.1");
Editor editor = sharedPreferences.edit();
editor.putString("area", "Home");
editor.commit();
setContentView(R.layout.activity_main);
setupDrawer();
if(sharedPreferences.getString("new", "Yes").equals("Yes")){
loadFiles();
}
private void loadFiles(){
if(isNetworkAvailable() == true){
ProgressDialog mProgressDialog;
// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading files...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(this);
downloadTask.execute();
mProgressDialog.show();
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}else{
new AlertDialog.Builder(this)
.setTitle("No internet detected")
.setMessage("Must have internet access to download files.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);
if(sharedPreferences.getString("new", "Yes").equals("Yes")){
((Activity) c).finish();
}
}
})
.show();
}
}
然后在新课程的底部我有:
private class DownloadTask extends AsyncTask<Integer, Void, Void> {
private Context context;
private PowerManager.WakeLock mWakeLock;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected Void doInBackground(Integer... sUrl) {
DataClass dc = new DataClass(c);
//ArrayList<String> menuArray = dc.getMenuArray("Home");
String[] menuArray = {"Home", "Announcements", "Speakers", "Events", "Exhibitors", "Agenda", "Hotels", "Documents"};
File dir = getDir("myDir", Context.MODE_PRIVATE);
for(int i=1; i <= 7; i++){
String[] menuList = handleMenuInfo(menuArray[i]);
for(int i2=1; i2 < menuList.length; i2++){
File file = new File(dir, menuArray[i] + "/" + menuList[i2] + ".txt");
ArrayList<String> viewArray = dc.getInfoArray(menuArray[i],menuList[i2]);
writeToFile(file, viewArray);
}
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);
Editor editor = sharedPreferences.edit();
editor.putString("new", "No");
editor.commit();
return null;
}
}
}
那么我可以在哪里放置代码来处理所有其他内容,例如进度和\或结束时关闭对话框?
答案 0 :(得分:0)
使用OnPreExecute
移动下载任务类中的进度条代码@Override
protected void onPreExecute() {
....progressbar code
}
然后在onPostExecute上用关闭
关闭它 @Override
protected void onPostExecute() {
mProgressDialog.dismiss();
}