SharedPreferences application context vs activity context

It is worth reviewing the sources that show that a Context instance (be it an Activity or an Application instance) share the same static map HashMap<String, SharedPreferencesImpl>. So whenever you request an instance of SharedPreferences by the same name via Context.getSharedPreferences(name, mode) you get the same instance since it first checks if the map already … Read more

How to call getResources() from a class which has no context?

A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. It is an “interface” that allows access to application specific resources and class and information about application environment. Your activities and services also extend Context to they inherit all those methods to … Read more

Is it possible to get application’s context in an Android Library Project?

There is one more way, add application class in your library project: /** * Base class for those who need to maintain global application state. */ public class LibApp extends Application { /** Instance of the current application. */ private static LibApp instance; /** * Constructor. */ public LibApp() { instance = this; } /** … Read more

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) … Read more