0

Hello i am writing a download project and i'm stuck right here.

@Override
protected void onProgressUpdate(Integer... progress) {

}

//I'm using in my mainactivity like that
Download download = new Download();
download.execute("http://");

I want to get progress when updated and yeah i can do this using onProgressUpdate but i wonder about can i get it from my mainactivity i mean where i called the class. I love that kinda dynamic classes because i can use them easily my every project. Thank you btw forgive me for my grammar.

  • http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660 – Lalit Poptani Aug 05 '13 at 12:04

2 Answers2

0

I can see two options here :

You either pass the object where you want to display the progress to your AsyncTask with the constructor and then you store it :

private ProgressObject mObject;
public void MyAsyncTask(ProgressObject object) {
   mObject = object
}

And the update in the onProgress() method :

object.setProgress();   //assuming the ProgressObject has a method setProgress() obviously

Or you can set up some kind of listener :

public interface ProgressListener() {
     public void onProgress(int progress);
}
private mProgressListener;
public void MyAsyncTask(ProgressListener listener) {
    mProgressListener = listener;
}

then use it in the onProgress() method :

mProgressListener.onProgress(80);

Just some examples, not much but I hope that help.

Aleks
  • 410
  • 4
  • 22
0

Use this

       class DownloadFileAsync extends AsyncTask<String, String, String> {

@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
    super.onPreExecute();

    showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
    int count;
    File root = android.os.Environment.getExternalStorageDirectory();               
     //
    File dir = new File (root.getAbsolutePath()+"/downoad");  //make ur folder to put download
    if(dir.exists()==false) {
             dir.mkdirs();
         }
    File file = new File(dir, "enter file name");
        try {

                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(file);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) 
                    {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        output.write(data, 0, count);
                    }

                output.flush();
                output.close();
                input.close();
        } catch (Exception e) {}
        return null;

}

protected void onProgressUpdate(String... progress) {
     Log.d("ANDRO_ASYNC",progress[0]);
     mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String unused) {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    Toast.makeText(DisplayActivity.this,"Successfully downloaded in phone memory.", Toast.LENGTH_SHORT).show();
}
   }

How to call?

    new DownloadFileAsync().execute("your URL");
WISHY
  • 11,067
  • 25
  • 105
  • 197