繁体   English   中英

什么时候以及为什么要在Java / Android中使用事件类型

[英]When and why should we use Event type in Java/Android

private class EarthquakeAsyncTask extends AsyncTask <String, Void, Event>{

  @Override
  protected Event doInBackground(String... urls) {
    // Perform the HTTP request for earthquake data and process the response.
    Event result = Utils.fetchEarthquakeData(urls[0]);
    return result;
  }

  protected void onPostExecute(Event result){
    // Update the information displayed to the user.
    updateUi(result);
  }

}

为什么在此代码片段中使用事件类型? 有人可以解释一下吗?

您可以将任何对象用于AsyncTask泛型类型的第三种类型。 对于从文档

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

  1. Params ,执行时发送给任务的参数类型。
  2. Progress ,后台计算期间发布的进度单位的类型。
  3. Result ,背景计算结果的类型。

对于以下课程:

private class EarthquakeAsyncTask extends AsyncTask <String, Void, Event>{
  ...
}

这意味着该类将String用作参数,将Void用作进度(这意味着它忽略进度),并使用Event作为doInBackground()方法中的过程完成时的结果。

EarthquakeAsyncTask的被调用方代码需要Event作为其参数:

protected void onPostExecute(Event result){
  // Update the information displayed to the user.
  updateUi(result);
}

因此,显然,EarthquakeAsyncTask需要将Event作为第三种类型。

暂无
暂无

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

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