Getting Context in Asynctask

getting context in AsyncTask

You need to do following things.

  • when you want to use AsyncTask, extend that in other class say MyCustomTask.
  • in constructor of new class, pass Context

Example

public class MyCustomTask extends AsyncTask<Void, Void, Long> {

private Context mContext;

public MyCustomTask (Context context){
mContext = context;
}

//other methods like onPreExecute etc.
protected void onPostExecute(Long result) {
Toast.makeText(mContext,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show();
}
}

And instantiate class by following.

MyCustomTask task = new MyCustomTask(context);
task.execute(..);

getApplicationContext in AsyncTask class?

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.

Context inside AsyncTask

I do not see you initialize your context memeber. Just add proper constructor:

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

and then instead of

AuthoriseMe authorise = new AuthoriseMe();

do

AuthoriseMe authorise = new AuthoriseMe(context);

and you are done. Alternatively you can just obtain the context by calling getApplication() or getApplicationContext() (be sure to read this question), which should be sufficient for most cases.

What is the best way of getting / using Context inside AsyncTask?

Simply create the AsyncTask as an inner class of your Activity, or pass the Context to the Constructor of the AsyncTask.

Inner class: MyActivity.java

public class MyActivity extends Activity {

// your other methods of the activity here...

private class MyTask extends AsyncTask<Void, Void, Void> {

protected Void doInBackground(Void... param) {

publishProgress(...); // this will call onProgressUpdate();
}

protected Void onProgressUpdate(Void... prog) {

Toast.makeText(getActivity(), "text", 1000).show();
}
}
}

Constructor: MyTask.java

public class MyTask extends AsyncTask<Void, Void, Void> {

Context c;

public MyTask(Context c) {
this.c = c;
}

protected Void doInBackground(Void... param) {

publishProgress(...); // this will call onProgressUpdate();
}

protected Void onProgressUpdate(Void... prog) {
Toast.makeText(c, "text", 1000).show();
}
}

Furthermore, please do not forget to call .show() on your Dialog.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.show();

How do i pass a context to an AsyncTask?

Pass a Context object into the AsyncTask's constructor.

Sample code:

public class MyTask extends AsyncTask<?, ? ,?> {
private Context mContext;

public MyTask(Context context) {
mContext = context;
}
}

and then, when you are constructing your AsyncTask:

MyTask task = new MyTask(this);
task.execute(...);

Passing context from Service to Asynctask without leaking it

I have a Service from which I am starting AsyncTask from a given timer to do background tasks.

Don't use an AsyncTask. Use a thread. Or, better yet, used ScheduledExecutorService for the timing component, as that will execute tasks on a background thread. AsyncTask is only appropriate for when you need to do work on the main application thread when the background part is done, and that's rarely required with a service.

Also, bear in mind that your timer will stop working once your process terminates.

So my question is, how can I use context in my AsyncTask(top level class) without leaking it?

Call getApplicationContext() on the Service and use that Context.

Android - define context in AsyncTask

First declare a Context variable in your fetchData class like this

Context context;

Now make a constructor for fetchData and take context as an argument like this

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

Then wherever you are creating an object of fetchData pass the context form that class like

fetchData fetchObject = new fetchData(this);
fetchObject.execute();

When you are using context for file it will be available in your fetchData class context variable. Hope it helps.



Related Topics



Leave a reply



Submit