ورود

View Full Version : لغو دانلود



ho3ein.3ven
دوشنبه 06 مرداد 1393, 01:01 صبح
سلام

من یه سورس دارم به صورت زیر :


public class MainActivity extends Activity {

// button to show progress dialog
Button btnShowProgress;


// Progress Dialog
private ProgressDialog pDialog;
VideoView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;


// File url to download
private static String file_url = "";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// show progress bar button
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
// Image view to show image after downloading
my_image = (VideoView) findViewById(R.id.my_image);


my_image.setMediaController(new MediaController(this));


/**
* Show Progress bar click event
* */
btnShowProgress.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
}
});
}


/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORI ZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}


/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {


/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}


/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();


// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);


// Output stream
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.3gp");


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


/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}


/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);


// Displaying downloaded image into image view
// Reading image path from sdcard
//String imagePath = Environment.getExternalStorageDirectory().toString () + "/downloadedfile.jpg";

//Uri vidFile = Uri.parse(
// Environment.getExternalStorageDirectory().getAbsol utePath()+"filename");


// setting downloaded into image view
//my_image.setImageDrawable(Drawable.createFromPath( imagePath));

// my_image.setVideoURI(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + "downloadedfile.3gp")));
my_image.setVideoPath("/sdcard/downloadedfile.3gp");

my_image.requestFocus();




my_image.start();
}


}


چطور میتونم با زدن دکمه back و با بسته شدن progress دانلود رو لغو کنم ؟

akbar8298
دوشنبه 06 مرداد 1393, 09:55 صبح
به جای اینکه توی onclick اینجوری کلاس دانلود رو صدا بزنید؛ یه شی سراسری تعریف کنید
مثل این
btnShowProgress.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// starting new Async Task
mDownloadFileFromURL = new DownloadFileFromURL().execute(file_url);

}
});

}




توی کلاس دانلودت هم توی حلقه این کد رو اضافه کنید

while ((count = input.read(data)) != -1 && !isCanceled()) {

موقع زدن کلید برگشت هم این کد رو صدا بزنید

mDownloadFileFromURL.cancel(true);

akbar8298
دوشنبه 06 مرداد 1393, 09:58 صبح
در ضمن پیشنهاد من استفاده از کلاس های دانلودر آماده هست که تست کارکرد رو گذروندند.

https://github.com/snowdream/android-downloader

ho3ein.3ven
دوشنبه 06 مرداد 1393, 12:15 عصر
در ضمن پیشنهاد من استفاده از کلاس های دانلودر آماده هست که تست کارکرد رو گذروندند.

https://github.com/snowdream/android-downloader

این سورس توی اکلیپس نیست ؟ چون ایمپورت نشد .