我有几个不同的AsyncTasks需要执行相同的共享基本操作,并在执行过程中显示进度。有没有办法通过传入另一个类来共享代码,而不是多次复制大量代码(每次都会显示进度)?抽象是非常不同的,你不能只传入AsyncTasks,因为它有一个受保护的publishProgress()方法。我看了一堆例子,并尝试了很多不同的变体,但都没有成功。
所以这里是details...lets,假设我们有两个不同的任务,做不同的事情,但都需要访问服务器,下载大量文件,然后将它们放到数据库中(然后做其他事情):
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线程和以doInBackground(对象...参数)。
我担心这是不可能的。
发布于 2015-05-06 06:57:20
robinj -正确,只是需要更好地阅读他/她的答案。诀窍是这样编码的:``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的帮助!如果你输入答案,我会竖起大拇指...
https://stackoverflow.com/questions/30064013
复制相似问题