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.

FD_
  • 12,947
  • 4
  • 35
  • 62
redrom
  • 11,502
  • 31
  • 157
  • 264

4 Answers4

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
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);
FD_
  • 12,947
  • 4
  • 35
  • 62
  • Is the array really needed in the `execute` call? – Giulio Piancastelli Jan 26 '14 at 10:29
  • Yes, it definitely is. – FD_ Jan 26 '14 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`. – Giulio Piancastelli Jan 26 '14 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 '14 at 10:44
  • 2
    Uh, no, it doesn't: if you look at http://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. – Giulio Piancastelli Jan 26 '14 at 10:51
  • Ok, sorry, remembered that wrong, you're right of course. Will fix that. – FD_ Jan 26 '14 at 11:21
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.

nKn
  • 13,691
  • 9
  • 45
  • 62
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.

Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35