我想開發文件下載器應用程序。該文件下載列表。當我將文件添加到下載程序列表,然後定期更新其進度。 我關閉了所有的活動,然後我運行應用程序,然後我的應用程序列表中的所有當前下載連續進度條讓下載器在後臺下載文件並上傳進度條
我該怎麼做?
我使用這個異步任務下載文件
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(
url.openStream(), 8192);
OutputStream output = null;
if (space_avaliable) {
File dir = new File(
Utill.saveFile(CustomGroupTabActivity.mTabHost
.getContext()) + "/tocaonline");
if (!dir.exists()) {
dir.mkdirs();
}
// Output stream to write file
output = new FileOutputStream(
Utill.saveFile(CustomGroupTabActivity.mTabHost
.getContext())
+ "/tocaonline/"
+ "test.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""
+ (int) ((total * 100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
}
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
p_bar.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(String file_url) {
}
}
當我上添加點擊下載按鈕,然後我加入到線性佈局動態視圖。
LinearLayout lytParent = (LinearLayout) findViewById(R.id.boxset_parentview);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = LayoutInflater.from(CustomGroupTabActivity.mTabHost
.getContext());
View convertview = inflater.inflate(R.layout.raw_set_detail, null);
final ProgressBar p_bar = (ProgressBar) convertview
.findViewById(R.id.rawsetdetails_sync_progress);
lytParent.addView(convertview);
當我的活動處於活動狀態時,它運行良好。但是當我關閉所有活動並再次運行此活動時,我想通過活動下載來顯示實際的下載進度。
發表你試過的東西。 – slezadav
請在我編輯的問題 –