从另一个类调用的AsyncTask [英] Call AsyncTask from another class
问题描述
在现有的应用程序,我有延伸AsyncTask的一个内部类的活动,这看起来像如下:
In an existing app I have an activity with an inner class which extends AsyncTask, this looks like the following:
public class Activity_1 extends BaseActivity {
....
new async().execute();
...
public class asyncextends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... progress) { ... }
protected void onPreExecute() { ... }
protected void onPostExecute(String result) { ... }
}
}
现在,我需要调用从另一项活动同样doInBackground法,但 onPostExecute()
这个内部类的工作在一些地方UI变量,因此的它的不可能从CLAS外面使用它。
有没有什么办法可以把这个AsyncTask的,只是重写 onPostExecute
和在preExecute
-method,或者应创建另一个内部类中的其他活动,做同样的背景事情(当然它移动到常用的实用工具类或某事),等等...?
Now, I need to call the same doInBackground-method from another activity, but the onPostExecute()
of the this inner class operates on some local UI variables and hence it's not possible to use it from outside the clas.
Is there any way I can call this AsyncTask, and just override the onPostExecute
andonPreExecute
-method, or shall I create yet another inner-class in the other activity, do the same background thing (of course move it to common utility-class or something), etc...?
推荐答案
您可以做一个单独的抽象封装私有类,延长的AsyncTask
和实施 doInBackground()
方法:
You can make a separate abstract package private class, extending AsyncTask
and implementing doInBackground()
method:
abstract class MyAsyncTask extends AsyncTask<Void, Void, String> {
@Override
final protected String doInBackground(Void... progress) {
// do stuff, common to both activities in here
}
}
和你们的活动距离 MyAsyncTask
继承(类新的可能应该是私有的,顺便说一句),实施 onPostExecute()
和在preExecute()
方法:
And in your activities just inherit from MyAsyncTask
(new class probably should be private, by the way), implementing onPostExecute()
and onPreExecute()
methods:
public class Activity_1 extends BaseActivity {
...
new Async1().execute();
...
private class Async1 extends MyAsyncTask {
@Override
protected void onPreExecute(){
// Activity 1 GUI stuff
}
@Override
protected void onPostExecute(String result) {
// Activity 1 GUI stuff
}
}
}
如果在preExecute
和 onPostExecute
包含一些常用的动作,以及,你可以使用以下方式
If onPreExecute
and onPostExecute
contain some common actions as well, you can apply the following pattern:
abstract class MyAsyncTask extends AsyncTask<Void, Void, String> {
public interface MyAsyncTaskListener {
void onPreExecuteConcluded();
void onPostExecuteConcluded(String result);
}
private MyAsyncTaskListener mListener;
final public void setListener(MyAsyncTaskListener listener) {
mListener = listener;
}
@Override
final protected String doInBackground(Void... progress) {
// do stuff, common to both activities in here
}
@Override
final protected void onPreExecute() {
// common stuff
...
if (mListener != null)
mListener.onPreExecuteConcluded();
}
@Override
final protected void onPostExecute(String result) {
// common stuff
...
if (mListener != null)
mListener.onPostExecuteConcluded(result);
}
}
和使用它在你的活动如下:
and use it in your activity as following:
public class Activity_1 extends BaseActivity {
...
MyAsyncTask aTask = new MyAsyncTask();
aTask.setListener(new MyAsyncTask.MyAsyncTaskListener() {
@Override
void onPreExecuteConcluded() {
// gui stuff
}
@Override
void onPostExecuteConcluded(String result) {
// gui stuff
}
});
aTask.execute();
...
}
您也可以有你的活动
实施 MyAsyncTaskListener
以及
You can also have your Activity
implement MyAsyncTaskListener
as well:
public class Activity_1 extends BaseActivity implements MyAsyncTask.MyAsyncTaskListener {
@Override
void onPreExecuteConcluded() {
// gui stuff
}
@Override
void onPostExecuteConcluded(String result) {
// gui stuff
}
...
MyAsyncTask aTask = new MyAsyncTask();
aTask.setListener(this);
aTask.execute();
...
}
我写了从头部code,所以它可能有错误,但它应该说明的想法。
I wrote the code from the head, so it might contain errors, but it should illustrate the idea.
这篇关于从另一个类调用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!