gpt4 book ai didi

安卓异步任务 : what is difference between execute() and get()?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:57 25 4
gpt4 key购买 nike

我应该这样写吗

return task.exec(session, state).get(json_timeout, TimeUnit.MILLISECONDS);

或者我可以这样做

task.exec(session, state, result);
return result;

A 已经阅读了我找到的所有文档,但未能找到答案。我的坏...

最佳答案

不要使用get()。它将阻塞 ui 线程,直到 asynctask 完成执行,这不再使其异步。

使用execute和调用asynctask

new task().exec(session, state, result);

您还可以将参数传递给 asynctask 的构造函数或传递给 doInbackground()

http://developer.android.com/reference/android/os/AsyncTask.html

public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

您可以将您的 asynctask 设为您的 Activity 类的内部类,并在 onPostExecute 中更新 ui。

如果 asynctask 在不同的文件中,那么你可以使用接口(interface)。

How do I return a boolean from AsyncTask?

关于安卓异步任务 : what is difference between execute() and get()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com