1

i trying to send email as assync task. But i don't know how exactly to pass different params into doInBackground?

I want to pass data like this:

Context ctx, String typeOfEmail , Map data

How can i pass it into AsyncTask class?

private class LongOperation extends AsyncTask<String, Void, String> {
 @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return "Executed";
        }

public boolean sendLogsByEmail(Context ctx, String typeOfEmail , Map<String, String> data) {

Thanks for any advice.

2

4 Answers 4

2

For more complex parameters you can add constructor:

public LongOperation (Context ctx, String typeOfEmail, Map data) {
  /// here initialize LongOperation class instance fields
}

also, its safier to put Context into WeakReference inside your AsyncTask, so it would look like:

WeakReference<Context> ctx;

and then use:

Context ctxe = ctx.get();
if ( ctxe != null ) {
   // do something with Context
}

this way you will not introduce reference leaks that might happend during configuration changes/or normal activity lifecycle. But since your AsyncTask is internal to activity class (I suppose from your code), then it already has implicit reference to enclosing class - which is probably Activity.

0

When you are defining an AsyncTask, you're extending its class. So nothing prevents you from implementing your own methods too, you could simply define:

public void my_vars(Context context, String str, Map map) { mycontext = context; mystr = str, mymap = map; }

and call it before calling your execute() method.

0

You can do like this

 new LongOperation().execute(new String[5]/* or add many parameter of string type*/); 

You can also create a arg constructor in class LongOperation and then pass within the constructor while creating obj.

0

Just declare the arguments to be of the common supertype of the objects you want to pass, which is Object.:

private class LongOperation extends AsyncTask<Object, Void, String> {
    @Override
    protected String doInBackground(Object... params) {
        Context c = (Context) params[0];
        String typeOfEmail = (String) params[1];
        Map data = (Map) params[2];

        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "Executed";
    }

Now call execute like this:

myLongOperation.execute(context, emailType, data);
6
  • Is the array really needed in the execute call? Jan 26, 2014 at 10:29
  • Yes, it definitely is.
    – FD_
    Jan 26, 2014 at 10:34
  • It baffles me why in this case I can't just pass the arguments one after the other, just like e.g. String.format. Jan 26, 2014 at 10:42
  • Because execute(), as defined by AsyncTask, always expects an array of objects of the class you set as the first Generic in the subclass declaration.
    – FD_
    Jan 26, 2014 at 10:44
  • 2
    Uh, no, it doesn't: if you look at developer.android.com/reference/android/os/AsyncTask.html, you will see it is defined with the same signature as doInBackground (with a varargs parameter, not an array) so passing arguments without enclosing them in an array should be possible. Jan 26, 2014 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.