Android:如何在AsyncTask完成之前暂停主线程? [英] Android : How can I pause the main thread till AsyncTask completes?

查看:192
本文介绍了Android:如何在AsyncTask完成之前暂停主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Android应用程序的主线程调用asynctask.但是,我需要暂停主线程,直到异步任务结束为止. 有人可以建议如何做吗?

I am calling an asynctask from the main thread of my Android application. However, I need to pause the main thread till the time the async task is over. Can someone suggest how to do this?

编辑:由于无法在主线程上执行网络请求,因此我需要使用AsyncTask

Edit : I need to use an AsyncTask, as I can not perform network requests on the main thread

推荐答案

您不能暂停主线程,但是可以等到AsyncTask完成并使用

You cannot pause the main thread but you can wait until AsyncTask complete and generate it's result by using

AsyncTask.get()

此方法将获取您在DoInBackground()中返回的结果,一旦任务完成,它将实现更多代码

this method will fetch the result you returning in the DoInBackground(), once the task finishes then it will implement the further code

例如,如果您返回字符串,然后像这样编写您的方法,

for example if you returning string then write your method like this,

String result=AsyncTask.get();

这篇关于Android:如何在AsyncTask完成之前暂停主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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