Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you pass multiple primitive parameters to AsyncTask?

There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain.

The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to an AsyncTask to be executed in the background--how can it be done? (My answer...after struggling with this for awhile...can be found below.)

like image 542
robguinness Avatar asked Aug 22 '12 09:08

robguinness


People also ask

What happen if we call execute more than once in AsyncTask?

Limitation Of AsyncTask There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with max number of worker threads (128) and the delayed tasks queue has fixed size 10. If you try to execute more than 138 AsyncTasks the app will crash with java.

How many methods are there for AsyncTask class?

AsyncTask's generic types The three types used by an asynchronous task are the following: Params , the type of the parameters sent to the task upon execution. Progress , the type of the progress units published during the background computation. Result , the type of the result of the background computation.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.


2 Answers

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); 
like image 177
David Wasser Avatar answered Sep 30 '22 23:09

David Wasser


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.

like image 36
malajisi Avatar answered Sep 30 '22 23:09

malajisi