我有一个列表视图。当用户选择列表视图中的项目时,我打开DetailActivity,用户可以在该活动中下载文件,并使用ProgressWheel
显示用户下载百分比。
我的问题是当用户关闭活动然后返回活动时,ProgressWheel
不再更新。
这是我的代码:(我使用AsynchTask下载文件)
/**
* Background Async Task to download file
* */
class DownloadTask extends AsyncTask<String, String, Boolean> {
/**
* Downloading file in background thread
* */
@Override
protected Boolean doInBackground(String... params) {
try {
URL url = null;
try {
String link = params[0];
fileExtention = getFileExtention(link);
url = new URL(link);
} catch (Exception e) {
e.printStackTrace();
return false;
}
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// set some parameters for httpUrlConnection
connection.setConnectTimeout(JSONConstants.CONNECTION_TIMEOUT);
connection.setReadTimeout(JSONConstants.READ_TIMEOUT);
connection.connect(); // Connection Complete here.!
// Get from Server and Catch In Input Stream Object.
InputStream inputStream = connection.getInputStream();
int lenghtOfFile = connection.getContentLength();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
File file = new File(PATH);
if (!file.exists()) {
file.mkdirs();
}
File outputFile = new File(file, "fileTitle"
+ ".mp3" );
FileOutputStream fileOutputStream = new FileOutputStream(
outputFile);
byte[] buffer = new byte[4096];
int length = 0;
long total = 0;
while ((length = inputStream.read(buffer)) != -1) {
total += length;
// publishing the progress....
// After this onProgressUpdate will be called
if (lenghtOfFile > 0)
publishProgress(""
+ (int) ((total * 100) / lenghtOfFile));
// Write In FileOutputStream.
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private void publishProgress(String percent) {
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
downloadProgressWheel.setProgress(Integer.valueOf(percent));
}
当用户返回其相关活动时,如何设置我的ProgressWheel
仍然更新。