android - AsyncTask<String, Void, String> 通用类型。安卓

标签 android android-asynctask

我已经在几个项目中使用这个 AsyncTask,但仍然不太明白 <String, Void, String> 的作用意思是。这些是否意味着未实现方法的参数类型?有什么命令吗(String、Void、String分别对应什么方法)?

最佳答案

来自 Android Docs AsyncTask页:

android.os.AsyncTask<Params, Progress, Result>

异步任务使用的三种类型如下:

Params, the type of the parameters sent to the task upon execution.

Progress, the type of the progress units published during the background computation.

Result, the type of the result of the background computation.

现在我用简单的话来理解:

参数:(在您的情况下为字符串)是 AsyncTask 的参数需要。调用execute时必须传递这个方法

进度:(在您的例子中为 Void)是进度的类型。 Void 表示您没有使用它。如果是整数,您可以使用 10、20、30 等值...并使用这些值在屏幕上显示进度条。

结果:(在您的情况下为字符串)是 AsyncTask 的内容作为结果返回。您正在返回一个字符串。您可以返回任何您想要的对象。

简单来说,有点像一个方法,Params是参数,Result是返回类型,progress告诉你处理进度的状态。

要进一步了解,请参阅 this tutorial , 也引用同一页可能会有帮助:

AsyncTask<TypeOfVarArgParams, ProgressValue, ResultValue>

TypeOfVarArgParams is passed into the doInBackground() method as input, ProgressValue is used for progress information and ResultValue must be returned from doInBackground() method and is passed to onPostExecute() as parameter.

关于android - AsyncTask<String, Void, String> 通用类型。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565208/

相关文章:

android - onPostExecute() 不能使用 MainActivity 元素

android - 如何检查Wifi已连接,但在Android中无法上网

android:从 url 加载后在显示中保留图像?

java - 我可以在布局或组件盒上设置凸起的边缘吗?

android - 在 asynytask 中完成 Activity Intent

java - Android - 任务在应该运行时没有运行

android - 使用 Android AsyncTask 和 Notification 卡住我的系统

android - Kotlin 协程返回字符串

android - 无法解析 : com. google.firebase :firebase-firestore:16. 0.1

java - setOnClickListener() 不起作用