在AsyncTask中的doBackgorund中的共享类中发布进度

时间:2015-05-05 22:02:33

标签: java android multithreading android-asynctask

我有几个不同的AsyncTasks需要执行相同的共享基本操作并在执行此操作时显示进度。而不是多次复制很多代码(每个代码都显示进度)有没有办法通过传入另一个类来共享代码? AsyncTasks非常不同,你不能只传入Abstract,它有一个受保护的publishProgress()方法。看了一堆例子,尝试了很多不同的变化而没有成功。

所以这里有详细信息......假设我们有两个不同的任务做不同的事情,但共享需要点击服务器,下载一些文件然后把它们放在一个数据库中(然后做其他的东西) :

public class ExampleTaskFragment extends ListFragment {

    private ProgressDialog mProgress;

    private void hideProgress() {
        mProgress.dismiss();
    }

    private void showProgress(String message) {
        mProgress = ProgressDialog.show(getActivity(), null, message, true, false);
    }

    protected void updateProgressMessage(String message) {
        mProgress.setMessage(message);
    }

    public class TaskA extends AsyncTask<Object, String, ArrayList<A>> {

        public void progressUpdate(String... values) {
             publishProgress(values);
        }

        @Override
         protected void onCancelled() {
              hideProgress();
         }
         @Override
         protected void onPreExecute() {
             showProgress("Stuff before shared...");
         }
         @Override
         protected ArrayList<A> doInBackground(Object... params) {

            // DoStuffPublish is the common stuff both tasks share
            DoStuffPublish dostuff = new DoStuffPublish(this);
            return doMoreStuffA();
         }

         @Override
         protected void onPostExecute(ArrayList<A> result) {
             updateProgressMessage("After shared stuff...");
             hideProgress();
         }  

         ArrayList<A> doMoreStuffA() {
            return new ArrayList<A>();
         }
    }

    public class TaskB extends AsyncTask<Object, String, ArrayList<B>> {

        public void progressUpdate(String... values) {
             publishProgress(values);
        }

        @Override
         protected void onCancelled() {
              hideProgress();
         }
         @Override
         protected void onPreExecute() {
             showProgress("Stuff before shared...");
         }
         @Override
         protected ArrayList<B> doInBackground(Object... params) {

            // DoStuffPublish is the common stuff both tasks share
            DoStuffPublish dostuff = new DoStuffPublish(this);
            return doMoreStuffB();
         }

         @Override
         protected void onPostExecute(ArrayList<B> result) {
             updateProgressMessage("After shared stuff...");
             hideProgress();
         }  

         ArrayList<B> doMoreStuffB() {
            return new ArrayList<B>();
         }
    }
}

public class DoStuffPublish {

    private AsyncTask task;

    public DoStuffPublish(AsyncTask task) {
        super();
        this.task = task;
        doSharedStuff();
    }

    protected void publishProgress(String message) {
        if (task instanceof TaskA) {
            ((TaskA)task).progressUpdate(message);
        }
        else if (task instanceof TaskB) {
            ((TaskB)task).progressUpdate(message);
        }
    }

    public  void doSharedStuff() {

        publishProgress("Doing Shared stuff...");

        // Do lots of shared things here...

        publishProgress("Done doing shared stuff...");

     }
}

这段代码拼凑在一起 - 所以不要过于依赖语法。它的概念......我希望有一个共享类,我可以调用2个或更多可以共享该代码并仍然发布更新的任务。我已经尝试将ProgressDialog放在任务中,但没有成功。当我这样做时,我得到“只有创建视图层次结构的原始线程才能触及它的视图”。在调试器中,您可以看到两个不同的线程 - 原始主UI线程和在Form doInBackground(Object ... params)中创建的线程。

我担心这可能不可能......

1 个答案:

答案 0 :(得分:0)

罗宾尼 - 说得对,只需更好地阅读他/她的答案。诀窍是像这样编码:`public class ExampleTaskFragment扩展ListFragment {

private ProgressDialog mProgress;

private void hideProgress() {
    mProgress.dismiss();
}

private void showProgress(String message) {
    mProgress = ProgressDialog.show(getActivity(), null, message, true, false);
}

protected void updateProgressMessage(String message) {
    mProgress.setMessage(message);
}

public class TaskA extends AsyncTask<Object, String, ArrayList<A>> {

    public void progressUpdate(String... values) {
         publishProgress(values);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        updateProgressMessage(values[0]);
    }
    @Override
     protected void onCancelled() {
          hideProgress();
     }
     @Override
     protected void onPreExecute() {
         showProgress("Stuff before shared...");
     }
     @Override
     protected ArrayList<A> doInBackground(Object... params) {

        // DoStuffPublish is the common stuff both tasks share
        DoStuffPublish dostuff = new DoStuffPublish(this);
        return doMoreStuffA();
     }

     @Override
     protected void onPostExecute(ArrayList<A> result) {
         updateProgressMessage("After shared stuff...");
         hideProgress();
     }  

     ArrayList<A> doMoreStuffA() {
        return new ArrayList<A>();
     }
}

public class TaskB extends AsyncTask<Object, String, ArrayList<B>> {

    public void progressUpdate(String... values) {
         publishProgress(values);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        updateProgressMessage(values[0]);
    }

    @Override
     protected void onCancelled() {
          hideProgress();
     }
     @Override
     protected void onPreExecute() {
         showProgress("Stuff before shared...");
     }
     @Override
     protected ArrayList<B> doInBackground(Object... params) {

        // DoStuffPublish is the common stuff both tasks share
        DoStuffPublish dostuff = new DoStuffPublish(this);
        return doMoreStuffB();
     }

     @Override
     protected void onPostExecute(ArrayList<B> result) {
         updateProgressMessage("After shared stuff...");
         hideProgress();
     }  

     ArrayList<B> doMoreStuffB() {
        return new ArrayList<B>();
     }
}

}

公共类DoStuffPublish {

private AsyncTask task;

public DoStuffPublish(AsyncTask task) {
    super();
    this.task = task;
    doSharedStuff();
}

protected void publishProgress(String message) {
    if (task instanceof TaskA) {
        ((TaskA)task).progressUpdate(message);
    }
    else if (task instanceof TaskB) {
        ((TaskB)task).progressUpdate(message);
    }
}

public  void doSharedStuff() {

    publishProgress("Doing Shared stuff...");

    // Do lots of shared things here...

    publishProgress("Done doing shared stuff...");

 }

} `

注意

    @Override
    protected void onProgressUpdate(String... values) {
        updateProgressMessage(values[0]);
    }

以前我一直试图直接设置mProgress值,但它是在错误的线程中?无论如何,感谢robinj的帮助!如果你回答我,我会竖起大拇指......