How can you pass multiple primitive parameters to AsyncTask?
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.
Android: Multiple params in Asynctask
try creating a constructor within your AsyncTask
and whenever you create your object you can pass the parameters.
Something like this:
MyAsyncTask asynctask = new MyAsyncTask(10, true, myObject);
//this is how you create your object
public class MyAsyncTask extends AsyncTask<Void, Void, Void>{
int a;
boolean b;
Object c;
public MyAsyncTask(int a, boolean b, Object c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
protected Void doInBackground(Void... params) {
if(b)
c = a;
return null;
}
}
and then you can just call asynctask.execute();
Edit: After reading your updated question I agree to Squonk to use a Service for example to play a background sound; also you can show a progress dialog before launching your AsynkTask (if this one is indefinetly) and dismiss it on your postexecute.
How can I pass a primitive int to my AsyncTask?
Pass your parameter as Integer
class proveAsync extends AsyncTask<Integer, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... position) {
int post = position[0].intValue();
}
.
.
.
while executing do this
new proveAsync().execute(new Integer(position));
You can get the int value in AsyncTask
as using intValue()
how to pass in two different data types to AsyncTask, Android
You should create a constructor for that.
public class UpdateInfoAsyncTask extends AsyncTask<Void, Void, Void>{
int intValue;
String strValue;
public UpdateInfoAsyncTask(int intValue,String strValue){
this.intValue = intValue;
this.strValue = strValue;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... params) {
//use intValue
//use strValue
return null;
}
}
To use it
new UpdateInfoAsyncTask(10,"hi").execute();
How can I pass in 2 parameters to a AsyncTask class?
Create a wrapper object to hold the two parameters, or just use something like an ArrayList or Dictionary and stick both your parameters in it.
Not pretty, but it works.
pass parameter to AsyncTaskString, Void, Void
soap_object.execute(new String []{"StringOne","StringTwo"});
You can also do :
soap_object.execute("StringOne","StringTwo");
In doInBackground
, params is a varargs argument, so just do :
execute__barcode_webservice(params[0], params[1]);
How to send values to another method
new GetDirecoes().execute(latRepr,LngRepr);
this is how you can pass.
private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String latRepr=params[0];
String LngRepr=params[1];
}
}
Using AsyncTask with multiple activities
You may try one host Activity with AsyncTask and two Fragments (authentication, chatting). Fragments will get result indirectly from the host Activity.
Be aware of AsyncTask pitfalls.
In your case the cleaner way is to use service.
Related Topics
Using Android Intent.Action_Send for Sending Email
Creating a PDF File in Android Programmatically and Writing in It
Android: Unable to Instantiate Activity/Classnotfoundexception
Android Update Textview in Thread and Runnable
How to Implement a Contentobserver for Call Logs
Android: How to Use Webcam in Emulator
Load an Image from Assets Folder
Using Listview:How to Add a Header View
How to Run Countdowntimer in a Service in Android
Switch Keyboard Profile Programmatically
How to Get Child of Child Value from Firebase in Android
How to Execute the Dex File in Android with Command
Disable Default Animation from Portrait to Landscape
Connecting to Wifi Using Adb Shell
How to Dynamically Create Menu Items
Android: How to Set the Zoom Level of Map View to 1 Km Radius Around My Current Location