简体   繁体   English

从asynctask调用函数

[英]call function from asynctask

Hello AsyncTask is not a member of activity. 您好AsyncTask不是活动的成员。 in separate class. 在单独的课程中。 how can i call function of activity from AsyncTask ? 如何从AsyncTask调用活动功能? thanks 谢谢

You can pass an instance of your activity in the constructor of your AsyncTask. 您可以在AsyncTask的构造函数中传递活动的实例。 But be sure to call the methods of activity only inside onPreExecute() and onPostExecute() methods, becuase they run on the UI Thread. 但是请确保仅在onPreExecute()onPostExecute()方法内部调用活动方法,因为它们在UI线程上运行。

If you want to call some activity code while doInBackground() , then you should create a Handler and pass it to the AsyncTask , because you can not modify the ui from non UI thread. 如果要在doInBackground()时调用一些活动代码,则应创建一个Handler并将其传递给AsyncTask ,因为无法从非UI线程修改ui。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM