How Do I Add An Asynctask To This?
I am creating a Android app and I am fairly new to threading in general I have two different methods that I use to call two different webservices as shown below, so how do I change
Solution 1:
You can extend your class from AsyncTask
and do like this:
publicclassAsyncCustomTaskextendsAsyncTask<Void, Void, List<String>> {
@OverrideprotectedList<String> doInBackground(Void... params) {
returngetEvacRouteNames();
}
@OverrideprotectedvoidonPostExecute(List<String> result) {
// Function finished and value has returned.
}
}
And to call it:
new AsyncCustomTask().execute();
Updated for second question
For the method that have parameters, you can use constructor of your class like:
publicclassAsyncSecondCustomTaskextendsAsyncTask<Void, Void, EvacRoute> {
private final String routeName;
private final LatLng currentLocation;
private final String lat;
private final String lon;
publicAsyncSecondCustomTask(String routeName, LatLng currentLocation, String lat, String lon) {
this.routeName = routeName;
this.currentLocation = currentLocation;
this.lat = lat;
this.lon = lon;
}
@OverrideprotectedEvacRoutedoInBackground(Void... params) {
returngetEvacuationRoute(routeName, currentLocation, lat, lon);
}
@OverrideprotectedvoidonPostExecute(EvacRoute result) {
// Function finished and value has returned.
}
}
And you can call it like:
new AsyncSecondCustomTask("", null, "", "").execute();
Post a Comment for "How Do I Add An Asynctask To This?"