简体   繁体   中英

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);
  }

}

Why use the type Event in this code snippet? Can someone please explain?

You can use any object for the third type of AsyncTask generic type. For From the documentation :

The three types used by an asynchronous task are the following:

  1. Params , the type of the parameters sent to the task upon execution.
  2. Progress , the type of the progress units published during the background computation.
  3. Result , the type of the result of the background computation.

For the following class:

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

it means that the class is using String as params, Void as progress (which means that it ignore the progress), and Event as the result when the process in doInBackground() method is finished.

the callee code for EarthquakeAsyncTask need Event as its parameter:

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

so, obviously, the EarthquakeAsyncTask need the Event as the third type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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