Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:11:39+00:00 2026-06-16T19:11:39+00:00

I have a question on using AsyncTask class provided by Android sdk. I am

  • 0

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[]{});
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:11:40+00:00Added an answer on June 16, 2026 at 7:11 pm

    When you start up a task that uses the context of an Activity, that task is being run in the same life cycle as the Activity. When the Activity is destroyed, its context is going to be lost with it, without a valid context the task will fail.

    If you want a context that is available for the lifetime of the application, you should use getApplicationContext() which does not require an active Activity (and shouldn’t be used to modify an Activity as a result).

    Incorrect usage can also cause issues with garbage collection – objects being left floating around.

    As has been mentioned in the comments section of your question, the best way to go forward, if you want to be able to run an AsyncTask whilst the application is not in the foreground (that is, in the background without the user’s input required), is to create a Service which the AsyncTask can run within.

    A Service will have its own context you can use, and you can bind the Service to your Activity if you want direct communication between the two.

    For more info on Services see this Android Developer Article which provides an overview of their use

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding using KVO-compliant methods to insert/remove objects from an array.
There is a similar question here, Activity using ViewPager, PagerAdapter and AsyncTask causes a
I have the question about using the AsyncTask with the ProgressDialog.There is the exception
Question arising from my first attempt at using an Async object. I have a
I have generic question on using JMS provided by JBoss 5.1. I deploy my
I have a question about using HTTPS in a JBossAS7 web application (a universal
i have a question about using GalleryView. At first, i set five default images
I have a question regarding using Semaphores HANDLE WINAPI CreateSemaphore(...); Is there anyway I
I have a question about using subqueries in an Update statement. My example: UPDATE
I have a question about using multiple .htaccess files - I couldn't find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.