wufoo
wufoo

Reputation: 14571

getApplicationContext in AsyncTask class?

I have a couple private AsyncTask methods that I'd like to break out into public external classes. In my application, I have a public class extending Application which holds some shared routines I want to access from different parts of my application. When I break out the AsyncTask class however, I'm no longer able to call getApplicationContext() to get a reference to my application library (the class does not inherit anything from Activity now). getBaseContext() will have problems as well. Should I be passing a context into the AsyncTask when it gets instantiated and build from there? Not sure if that was safe or not.

import java.util.ArrayList;
import android.os.AsyncTask;

public class DlTask extends AsyncTask
{
    long totalbytes = 0;
    long totalread = 0;
    ArrayList <String> data;

    @Override
    protected void onPreExecute ()
    {   
        AppLib lib = (AppLib) getApplicationContext();

        lib.saveStatusText ("Contacting " + lib.getServerAddress () + ":" + lib.getServerPort () + "...");
       super.onPreExecute ();
    }

      @Override
      protected Object doInBackground (Object... params)
      {
        data = lib.sendCommand (CMD_LIST);
         return true;
      }

      @Override
      protected void onPostExecute (Object result)
      {

         if (data != null)
         {
                    lib.saveStatusText (data.size () + " files found");
         }
         else
         {
            Log.d (TAG, "data is null");
            Toast.makeText (getBaseContext(), msg, Toast.LENGTH_SHORT).show ();                     
         }

         super.onPostExecute(result);
      }

}

Upvotes: 13

Views: 13556

Answers (3)

Rodrigo Xavier
Rodrigo Xavier

Reputation: 222

You should do like this. In my case it works:

public class DlTask extends AsyncTask {
     private Context context;

     public DlTask(Context context) { this.context = context; }

     @Override
     protected void onPreExecute() {
         super.onPreExecute();
         ..........
     }

     @Override
     protected Object doInBackground (Object... params)
     {
        data = lib.sendCommand (CMD_LIST);

        return true;
     }

     @Override
     protected void onPostExecute (Object result)
     {

        if (data != null)
     {
         lib.saveStatusText (data.size () + " files found");
     }
     else
     {
        Log.d (TAG, "data is null");
        Toast.makeText (context, msg, Toast.LENGTH_SHORT).show ();              
        //You can use the CONTEXT like this (passing the context variable like parameter
     }

     super.onPostExecute(result);
  }

Upvotes: 1

SimonSays
SimonSays

Reputation: 10977

Either pass a Context in the constructor of your AsyncTask, or use a static variable in your Application class to access it (set sMyInstace = this;). The first solution is preferable though.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007296

Should I be passing a context into the AsyncTask when it gets instantiated and build from there?

You do not have a choice, as you will be unable to get a Context by any other means.

Upvotes: 13

Related Questions