我正在从服务器下载文件,然后解析并插入本地数据库,而此操作我正在使用进度条显示操作进度。由于文件非常大,因此解析需要 2-4 分钟,因此我在进度条上收到 ANR下载完成后进度条也没有更新,它只显示 50% 的进度。如何避免这种 ANR?我附上我的代码如下...
@Override
protected void onPreExecute()
{
flag = false;
progressCount = 0;
ProgressDialog progressBar = DdownoadProgressBar(context,
progressCount);
progressBar.show();
}
@Override
protected Integer doInBackground(String... urls) {
checkNetworkConnection();
if (connectionFlag) {
downloadFile1();
progressCount = 1;
publishProgress(progressCount);
downloadFile2();
progressCount = 2;
publishProgress(progressCount);
downloadFile3();
progressCount = 3;
publishProgress(progressCount);
downloadFile4();
progressCount = 4;
publishProgress(progressCount);
downloadFile5();
progressCount = 5;
publishProgress(progressCount);
downloadFile6();
progressCount = 6;
publishProgress(progressCount);
downloadflag = true;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
Log.v(TAG, "In progressUpdate...");
progressBar.setProgress(progressCount);
}
@Override
protected void onPostExecute(Integer progress) {
if (downloadflag) {
try {
Log.v(TAG, "Inside onPostExecute....");
parseFile1();
progressCount=7;
publishProgress(progressCount);
parseFile2();
progressCount=8;
publishProgress(progressCount);
parseFile3();
progressCount=9;
publishProgress(progressCount);
parseFile4();
progressCount=10;
publishProgress(progressCount);
parseFile5();
progressCount=11;
publishProgress(progressCount);
parseFile6();
progressCount=12;
publishProgress(progressCount);
progressBar.dismiss();
Toast.makeText(SynchronizeData.this,"Data is successfully downloaded from server.......", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
} else {
progressBar.dismiss();
Toast.makeText(
getApplicationContext(),
"File is not downloaded....Error in network connection...........",Toast.LENGTH_LONG).show();
Log.v(TAG, "File is not downloaded...............");
}
}