从AsyncTask外部调用AsyncTask onPostExecute [英] AsyncTask onPostExecute call from outside the AsyncTask

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

问题描述

我想在我的应用程序的不同位置使用相同的AsyncTask,并且我想为每个应用程序使用不同的 onPostExecute .是否可以通过 onCreate 方法而不是AsyncTask类来调用它们?

I want to use the same AsyncTask in different places on my App and I want a different onPostExecute for each one. It's possible to call them from onCreate method instead of from AsyncTask class?

类似的东西:

new AsyncTCP.execute()
onPostExecute(){
//DO STUFF
}

推荐答案

只需创建一个AsyncTask,且未覆盖任何onPostExecute方法

Just create an AsyncTask with no onPostExecute method overridden

public class YourTask extends AsyncTask<Whatever, Whatever, Whatever> {

    @Override
    protected Whatever doInBackground(Whatever... whatever) {
        // whatever
        return whatever;
    }

}

然后,当您需要onPostExecute的不同实现时,创建匿名任务或子孙类并使用它

And then when you want different implementations of onPostExecute, create anonymous task or descendant class and use it

task = new YourTask() {

    @Override
    protected void onPostExcecute(Whatever result) {
        //whatever
    }

};
task.execute();

更好

public class YourTaskAnother extends YourTask {

    // just override onPostExecute here

    @Override
    protected void onPostExcecute(Whatever result) {
        //whatever
    }
}

task = new YourTaskAnother();
task.execute();

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

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