0

I would like to send and email on the background of my app. I followed this example and implemented it http://javapapers.com/android/android-email-app-with-gmail-smtp-using-javamail/

public class SendMailTask extends AsyncTask {

    private ProgressDialog statusDialog;
    private Activity sendMailActivity;

    public SendMailTask(Activity activity) {
        sendMailActivity = activity;

    }

    protected void onPreExecute() {
        statusDialog = new ProgressDialog(sendMailActivity);
        statusDialog.setMessage("Getting ready...");
        statusDialog.setIndeterminate(false);
        statusDialog.setCancelable(false);
        statusDialog.show();
    }

    @Override
    protected Object doInBackground(Object... args) {
        try {
            Log.i("SendMailTask", "About to instantiate GMail...");
            publishProgress("Processing input....");
            GMail androidEmail = new GMail(args[0].toString(),
                    args[1].toString(), (List) args[2], args[3].toString(),
                    args[4].toString());
            publishProgress("Preparing mail message....");
            androidEmail.createEmailMessage();
            publishProgress("Sending email....");
            androidEmail.sendEmail();
            publishProgress("Email Sent.");
            Log.i("SendMailTask", "Mail Sent.");
        } catch (Exception e) {
            publishProgress(e.getMessage());
            Log.e("SendMailTask", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public void onProgressUpdate(Object... values) {
        statusDialog.setMessage(values[0].toString());

    }

    @Override
    public void onPostExecute(Object result) {
        statusDialog.dismiss();
    }

}

The code is working fine
However, i have a presentation and i need to explain the code. In the code, SendMailTask extends AsyncTask without any extra parameters not even Void

I m stuck in this point because i searched and no one is using this way. Can anyone explain it to me?

Tim
  • 41,901
  • 18
  • 127
  • 145
Arwa Moath
  • 59
  • 1
  • 1
  • 5

3 Answers3

0

If you not specify any Generic parameter , You will see it will take Object as a type (Base Class for all Classes)

So in case of using only AsyncTask

You are actually dealing with Object

eg- See the parameter of doInBackground() in the given link

Gaurav
  • 3,615
  • 2
  • 27
  • 50
0

without the parameters, AsyncTask will assume the default class (which is Object)

public class SendMailTask extends AsyncTask <Object1, Object2, Object3> {

    @Override
    protected Object doInBackground(Object1... args) {
    ...    

       //publishProgress calls "onPublishProgress" method
       //e.g. publishProgress("email sent.");  
       publishProgress(object2); 

       //last line returns to "onPostExecute" method
       //e.g. return null;
       return object3; 
    }

    @Override
    public void onProgressUpdate(Object2... values) {
    ...

    }

    @Override
    public void onPostExecute(Object3 result) {
    ...
    } 
}

Object1 is the array of parameters you pass in when initializing the asyncTask

            new SendMailTask(SendMailActivity.this).execute(fromEmail,
                    fromPassword, toEmailList, emailSubject, emailBody);

so fromEmail, fromPassword, etc, etc all goes into the array of Object1. you access them in doInBackground method using arg[ index ].

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0

Honestly, the way they've extended AsyncTask isn't terribly smart. It causes a number of annoying warnings in Android Studio, and it would have been just as easy to extend AsyncTask<Object, Object, Object>, it's the same effect, but it removes the warnings.

All of that said, in order to use the class, you can simply call the following:

Object[] args = new Object[]{
    arg1,
    arg2,
    etc
};
new SendMailTask(activity).execute(args);

You just need to check the GMail constructor to see how you should order your args.

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28