从另一个类调用 AsyncTask [英] Call AsyncTask from another class

查看:15
本文介绍了从另一个类调用 AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在现有的应用程序中,我有一个带有扩展 AsyncTask 的内部类的 Activity,如下所示:

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-method,但是这个内部类的 onPostExecute() 对一些本地 UI 变量进行操作,因此不可能从课外.有什么办法可以调用这个 AsyncTask,并且只覆盖 onPostExecuteonPreExecute-方法,或者我应该在另一个活动中创建另一个内部类,执行相同的背景事物(当然将其移至通用实用程序类或其他东西)等......?

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()onPreExecute() 方法:

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

如果 onPreExecuteonPostExecute 也包含一些常见的操作,您可以应用以下模式:

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();
    ...
}    

您也可以让您的 Activity 也实现 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();
    ...

}

我从头开始编写代码,所以它可能包含错误,但它应该说明这个想法.

I wrote the code from the head, so it might contain errors, but it should illustrate the idea.

这篇关于从另一个类调用 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆