I have a question on using AsyncTask
class provided by Android sdk. I am starting a task in my code from the activity whose work is to send emails periodically (as per the specified time). I restart the task in onPostExecute()
. It does send email periodically but after some time emails stop going. Does pressing the back button have any impact on it ?
I was going through the following link on AsyncTask and found that AsyncTask
needs to be refreshed after the activities' orientation changes or is out of focus. Do I need to handle this separately ? Do i need to refresh the context
everytime the activity
is out of focus or its orientation changes ? There are certain DB operations I am doing based on context.
Here is my AsyncTask
code :
public class SendEmailTask extends AsyncTask<String, Void, String> {
private static final String LOG_TAG = "EmailTask";
private static final int MESSAGE_SENT = StringConstants.CONSTANT_YES_FLAG;
private Context context;
public SendEmailTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... time) {
// String message = "Message sent at ";
try{
//DB operations
Validator validator = new Validator(context);
int emailInterval = validator.validForSendingEmail(settingsMap);
String emailId = settingsMap.get(DBSetting.COLUMN_EMAILID);
String emailPwd = settingsMap.get(DBSetting.COLUMN_EMAIL_PWD);
if (emailId != null && emailPwd != null && emailInterval > 0) {
Thread.sleep((Integer.valueOf(emailInterval) * 60000));
// TODO: formatting of email body
DALLog dalLog = DALLog.getDALLogInstance();
dalLog.init(context);
GMailSender sender = new GMailSender(emailId, emailPwd);
sender.sendMail("Mail From Auto responder",
result, emailId,
emailId);
dalLog.close();
}
return null;
}
catch (NumberFormatException e) {
e.printStackTrace();
Log.d(LOG_TAG, e.getMessage());
}
catch (InterruptedException e) {
e.printStackTrace();
Log.d(LOG_TAG, e.getMessage());
}
catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
//DB operations
Validator validator = new Validator(context);
int emailInterval = validator.validForSendingEmail(settingsMap);
// Start EmailTask thread if not started already
SendEmailTask emailTask = new SendEmailTask(context);
if (emailTask.getStatus() != AsyncTask.Status.RUNNING) {
emailTask.execute(new String[]{});
}
}
}