Pass the context as a parameter to the AsyncTask constructor and save it there as a member. But be careful what type of context you pass to the constructor.
When a task can run during the Activity lifecycle, you must pass in the application context instead of the Activity context. When a task runs just for the life of an Activity, you can pass an Activity object as a context.
source share