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());
}
}
من از طریق 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());
}
}