It is (strictly-speaking) NOT possible to pass multiple primitives to AsyncTask. For example, if you want to perform myTask.execute(long1, long2)
and try to set up private class myTask extends AsyncTask<long, Void, Void>
with the corresponding method:
@Override
protected LocationItemizedOverlay doInBackground(long... params) {...}
your IDE will likely complain about needing to override a supertype method. Note that you are using the so-called Varargs method signature for doInBackground
, where (long... params)
is like saying "I accept a variable number of longs, stored as an array called params. I don't completely understand what causes a compiler/IDE complaint to be raised, but I think it has to do with how the generic class Params
is defined.
In any case, it is possible to achieve what you want with no problem, provided you correctly cast your primitives to their respective non-primitive wrappers (e.g. int => Integer, long => Long, etc.). Actually, you don't need to explicitly cast your primitives to non-primitives. Java seems to handle that for you. You just need to set up your ASyncTask as follows (for the example of longs):
private class MyTask extends AsyncTask<Long, Void, Void> {
@Override
protected void doInBackground(Long... params) {
// Do stuff with params, for example:
long myFirstParam = params[0]
}
...
}
You can then use this class as you originally intended, e.g.:
MyTask myTask = new MyTask();
myTask.execute(long1, long2);
Or for any number of primitives that you would like, PROVIDED THEY ARE OF THE SAME TYPE. If you need to pass multiple types of primitives, this can also be done, but you will need to modify the above to:
private class MyTask extends AsyncTask<Object, Void, Void> {
@Override
protected void doInBackground(Object... params) {
// Do stuff with params, for example:
long myLongParam = (Long) params[0];
int myIntParam = (Integer) params[1];
}
...
}
This is more flexible, but it requires explicitly casting the parameters to their respective types. If this flexibility is not needed (i.e. a single data type), I recommend sticking to the first option, as it's slightly more readable.
protected LocationItemizedOverlay doInBackground(Object[] objects)
and add the following for the async task definition.private class MyTask extends AsyncTask<Object, Void, Void>
– Eneidaenema