PDA

View Full Version : دانلود , progress bar بوسیله AsyncTask



mojtabadj
سه شنبه 25 آذر 1393, 18:43 عصر
سلام دوستان عزیز

من از طریق AsyncTask یک کلاس نوشتم برای دانلود فایلها !

مثل نرم افزار بازار دکمه دانلود من تو هر activity هستش ! زمانی که دکمه دانلود زده میشه تو نوتیفیکیشن نمایش داده میشه و progress bar تو activity هم

جلو میره ، ولی زمانی که از activity خارج میشم و دوباره وارد میشم دیگه progress bar رو نمیشناسه و جلو نمیره واسه اینکار باید چیکار کنم؟



private class Downloader extends AsyncTask<Void, Integer, Integer> {

private final Activity parent;
private final ProgressBar progress;
private final TextView textview;
private final String file_url;
private final String output_url;

private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;

private String NewName ;
private int DownloadPercent;

int _NotifyID=0;

public Downloader(int NotifyID ,final Activity parent, final ProgressBar progress, final TextView textview,String file_url,String output_url) {
this.parent = parent;
this.progress = progress;
this.textview = textview;
this.file_url = file_url;
this.output_url = output_url;
_NotifyID = NotifyID;
this.NewName = "";
this.DownloadPercent = 0;
}

@Override
protected void onPreExecute() {
super.onPreExecute();


// Setup an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(getActivity().getApplicationContext(), LessonDetailsActivity.class);
resultIntent.putExtra("lesson_id", lesson_id);
// TaskStackBuilder ensures that the back button follows the recommended convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(LessonDetailsActivity. class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
//PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent resultPendingIntent = PendingIntent.getActivity(
getActivity().getApplicationContext(), _NotifyID, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder = new NotificationCompat.Builder(getActivity());
mBuilder.setContentTitle(FileName)
.setContentText("Download in progress :" + FileName)
.setSmallIcon(R.drawable.ic_download)
.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
mBuilder.setProgress(0, 0, true);//Set to true, said flow
mNotifyManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATIO N_SERVICE);

// Displays the progress bar for the first time.
mBuilder.setProgress(100, 0, false);
if(isProcessRunning)
progress.setMax(100);
mNotifyManager.notify(_NotifyID, mBuilder.build());
}
@Override
protected void onCancelled() {
if(isProcessRunning)
progress.setMax(0); // stop the progress
Log.v("Progress","Cancelled");
}

@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(_NotifyID, mBuilder.build());
DownloadPercent = values[0];
//mNotifyManager.notify(1, mBuilder.build());
if(isProcessRunning){
progress.setProgress(values[0]);
textview.setText(String.valueOf(values[0]));
}

super.onProgressUpdate(values);
}

@Override
protected Integer doInBackground(Void... params) {
int count;
try {
URL url = new URL(file_url);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();

// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);

// Output stream to write file

//OutputStream output = new FileOutputStream("/sdcard/"+output_url);
//NewName = String.valueOf(System.currentTimeMillis());
NewName = MD5(output_url);
OutputStream output = new FileOutputStream("/sdcard/"+ NewName);

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;
}

@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
DownloadPercent = 0;
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(_NotifyID, mBuilder.build());
}
}

mojtabadj
چهارشنبه 26 آذر 1393, 08:06 صبح
وقتی یک اکتیویتی رو باز میکنیم از ابجکت ها نمونه جدید میسازه؟ !

mojtabadj
پنج شنبه 27 آذر 1393, 08:47 صبح
چه فروم فعالیه اینجا :قهقهه:

Nevercom
پنج شنبه 27 آذر 1393, 13:38 عصر
خب شما اگر با Life Cycle برنامه های اندرویدی آشنایی داشته باشید، می دونید که کاری که نیاز هست مستقل از طول عمر خود اپلیکیشن انجام بشه رو در Service انجام بدیم.
در مورد Progress Bar، در روشی که شما استفاده می کنید، رفرنسی از خود ProgressBar رو کلاس AsyncTask می فرستید تا آپدیتش کنه بصورت مستقیم.

راه حلی که به ذهن من میرسه اینه که یک کانال ارتباطی با سرویسی که دانلود رو مدیریت می کنه ایجاد کنید، مثلاً BroadcastReceiver.
هر درخواست دانلود یک ID منحصربفرد خواهد داشت که با اون ID میشه وضعیت دانلودی که اون کلاس درخواستش داده رو پیگیری کرد.

در این الگو هربار که اکتیویتی ایجاد میشه، یک BroadcastReceiver رو رجیستر می کنه و ID ش رو هم اعلام می کنه، در نتیجه به محض ایجاد اکتیوتی اگر درخواستی درحال اجرا داشته باشه، درصد پیشرفت بهش اطلاع داده میشه.

اینها رو هم پیدا کردم که کمکتون می کنه:
http://stackoverflow.com/questions/13214145/progressbar-and-asynctask

و این کتابخانه که خیلی جذاب بنظر میرسه و در همون لینک قبلی معرفی شده:
https://github.com/stephanenicolas/robospice

mojtabadj
چهارشنبه 03 دی 1393, 12:13 عصر
مرسی از جوابت دوست عزیز

من یه مشکلی زمان دانلود دارم اونم اینه که ، زمان شروع دانلود گوشیم میره تو حالت هنگ ، خیلی سرعتش پایین میاد باید چیکار کنم؟