0

I have asyncTask class:

public class DownloadJar {


    public DownloadJar(String serialnum) 
    {

         if (serialnum!=null) 
         {
             this.serialnum = serialnum;         
             new Download().execute();
         }  
    }

    private class Download extends AsyncTask<String, Integer, String>
    {

        @Override
        protected String doInBackground(String... params) {

         //Downloading stuff

            return null;
        }

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

            //Need to notify main class
            super.onProgressUpdate(values);
        }

    }
}

I activate it via main class:

    public class FTPDownload extends Activity{  

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ftpdownload);


        Bundle extras = getIntent().getExtras();
        if (extras != null) 
        {
            serialnum = extras.getString("serialnum");
        }

         if (serialnum!=null) 
         {

            DownloadJar dj = new DownloadJar(serialnum);
         }       

    }
}

How can I create callback from the DownloadJar to notify my main class about the progress?

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
Dim
  • 4,527
  • 15
  • 80
  • 139

4 Answers4

1

If you need onProgressUpdate() inside your main class, override it there. like,

DownloadJar dj = new DownloadJar(serialnum){
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }
};
Nizam
  • 5,698
  • 9
  • 45
  • 57
1

Pass a handler created in the activity along with the serialnumString in the constructor of the DownloadJar Async and call it like this from your onProgressUpdate:

handler.sendEmptyMessage(0);

where you can pass data through messages from your AsyncTask, you can receive the events in your Activity like this

handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    //do something 
  }

};

For further info: http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

MariusBudin
  • 1,277
  • 1
  • 10
  • 21
1

You can create interface ie:

interface UpdateProgress {
 void progressUpdate(String s)
}

make your class FTPDownload (its your main class?) implement it. Then when creating DownloadJar, pass reference to your activity to it. Then you can call in onProgressUpdate from your DownloadJar.Download asynctask progressUpdate on main activity. You must remember to update this reference on configuration changes.

If on the other hand DownloadJar is your main class, then you already have access to it, your asynctask is internal to it.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

you have make Interface so that you can set data in that interface method from onPostExecute method of AsyncTask. and pass interface method as Anonymous inner class as like onClickListner.

here you can find nice explanation.

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75