如何在 onProgressUpdate AsyncTask 时从另一个 class 获取值?

How to get value while onProgressUpdate AsyncTask from another class?

我通常在与调用者相同的 class 中使用 AsyncTask,但现在我想使用接口 class 从不同的 class 调用 asyntask,我想从 onProgressUpdate 获取值来自另一个类的调用,这是我的异步任务 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();
  }
}

在我的 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);
        }
}

我使用接口 class 与不同的 class 进行通信,这里是我的接口代码

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

我该如何解决这个问题,也许还有其他方法,请帮忙

谢谢

你应该在异步任务中设置监听器吧?

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

并在您的 activity 中开始初始化您的异步任务:

UploadFileToServer uFTS=new UploadFileToServer(url,file,myActivity.this,myActivity.this);
//then execute:
uFTS.execute();