Instead of doing it by the hard way as you are, just use a Singleton extending your Application
. Something like this:
import android.app.Application;
import android.content.Context;
public class MyAppContext extends Application {
private Context context;
public Context getContext() { return context; }
public void setContext(Context context_) { context = context_; }
public void onCreate(){
super.onCreate();
this.context = getApplicationContext();
}
}
Afterwards, if your want to get your context, simply use:
MyAppContext myContextManager = ((MyAppContext) getApplicationContext());
---- EDIT ----
Once you create your MainActivity, simply call:
myContextManager.setContext(this);
You do this just once. The next times you only need to get it. In your BroadcastReceiver
you won't be able to do this, so get it prior to defining it and store it within a variable (for example, called myContextManager
), and inside just do something like:
Context context = myContextManager.getContext();