0

I usually use AsyncTask in the same class with the caller, but now I want to call asyntask from different class using interface class,I want to get value from onProgressUpdate call from another clas, this my asynctask class..

class UploadFileToServer extends AsyncTask<String, Integer, String> implements DialogInterface.OnCancelListener {
      private String url;
      private File file;
      ShareProgress shareProgres;
      SharePosExecute sharePosExecute;
       public UploadFileToServer(String url, String file) {

        this.url = url;
        this.file = new File(file); 
       }

   @Override
  protected void onPreExecute() {

  }

   @Override
  protected String doInBackground(String... v) {
    //place code of upload file to server,run very well...
    return responseFromServer;
  }

   @Override
  protected void onProgressUpdate(Integer... progress) {
    Log.v("progress",progress[0]+""); //this work well
    shareProgres.progressUpdate(progress[0]); //I get error in here

  }

   @Override
  protected void onPostExecute(String response) {
       sharePosExecute.posExecute(response); // i also get error in here
  }

   @Override
  public void onCancel(DialogInterface dialog) {
    cancel(true);
   // dialog.dismiss();
  }
}

in my activity class

public class myActivity extends SherlockFragmentActivity implements ShareProgress,SharePosExecute {


        @Override
        public void progressUpdate(int progress) {
             Log.i("progress in myactivity",progress+""); //i want to this code work.

        }
        @Override
        public void posExecute(String output) {
            Log.i("processFinish myActivity",output);
        }
}

I use interface class for comunicated with diferent class, here my interface code

public interface ShareProgress {
     void progressUpdate(int progress);
}

how do i fix this,may be there is another ways for this, please help

thanks

ltvie
  • 931
  • 4
  • 19
  • 48

1 Answers1

0

you should be setting your Listener in your asynctask right?

    ShareProgress shareProgres;
      SharePosExecute sharePosExecute;
       public UploadFileToServer(String url, String file,ShareProgress shareProgres,SharePosExecute sharePosExecute) {

        this.url = url;
        this.file = new File(file); 
        this.shareProgres=shareProgres;
        this.sharePosExecute=sharePosExecute;
       }

and in your activity,start initialize your async task:

UploadFileToServer uFTS=new UploadFileToServer(url,file,myActivity.this,myActivity.this);
//then execute:
uFTS.execute();
Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • right... I have done according to your answers..,successfully . thanks. – ltvie Feb 26 '15 at 13:09
  • You're welcome .however if you googled a bit ,the [android developer site](http://developer.android.com/reference/android/os/AsyncTask.html) and [this SO answer](http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui) would help you quite a bit. – Droidekas Feb 27 '15 at 04:55