How can you pass multiple primitive parameters to AsyncTask?
Just wrap your primitives in a simple container and pass that as a parameter to AsyncTask
, like this:
private static class MyTaskParams {
int foo;
long bar;
double arple;
MyTaskParams(int foo, long bar, double arple) {
this.foo = foo;
this.bar = bar;
this.arple = arple;
}
}
private class MyTask extends AsyncTask<MyTaskParams, Void, Void> {
@Override
protected void doInBackground(MyTaskParams... params) {
int foo = params[0].foo;
long bar = params[0].bar;
double arple = params[0].arple;
...
}
}
Call it like this:
MyTaskParams params = new MyTaskParams(foo, bar, arple);
MyTask myTask = new MyTask();
myTask.execute(params);
Another way: You just need add MyTask constructor in your MyTask class:
private class MyTask extends AsyncTask<String, Void, Void> {
int foo;
long bar;
double arple;
MyTask(int foo, long bar, double arple) {
// list all the parameters like in normal class define
this.foo = foo;
this.bar = bar;
this.arple = arple;
}
...... // Here is doInBackground etc. as you did before
}
Then call
new MyTask(int foo, long bar, double arple).execute();
A second way like David Wasser's Answer.