如何将AsyncTask的onpostexecute方法的结果传递到父活动的android如何将、结果、方法、onpostexecute

2023-09-07 08:34:17 作者:老酒才对味

我正在开发中,我需要发送的AsyncTask的的onPostExecute方法的结果的值到previous活动的应用程序,即在该aync任务是called.pls把一些codeS活动。 Anyhelp是AP preciated

I am developing an application in which i need to send the value of the asynctask's onPostExecute method's result in to the previous activity , ie the activity in which the aync task is being called.pls put some codes. Anyhelp is appreciated

推荐答案

有两种方式:

在声明类的父活动延伸AsyncTask的私有类 通处理器或活动本身作为类扩展的AsyncTask的参数

如果我是你,我会跟着第一个选项。 看看文档:

If I were you, I'd follow the first option. Look at DOCS:

class MyActivitySubclass extends Activity {

    function runOnPostExecute(){
        // whatever
    }

    private class MyTask extends AsyncTask<Void, Void, Void> { 

        void doInBackground(Void... params){
            // do your background stuff
        }

        void onPostExecute(Void... result){
            runOnPostExecute();
        }

    }

}

注1

code放置在身体的机能 onPostExecute 已在活动线程上运行,你应该提到的 这个 关键字导致 MyTask.this ,而不是 MyActivitySubclass.this

Code placed in body of function onPostExecute is already run on Activity thread, you should just mention that this keywords leads to MyTask.this and not MyActivitySubclass.this