How to get package name from anywhere?

An idea is to have a static variable in your main activity, instantiated to be the package name. Then just reference that variable. You will have to initialize it in the main activity’s onCreate() method: Global to the class: public static String PACKAGE_NAME; Then.. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PACKAGE_NAME = getApplicationContext().getPackageName(); … Read more

getApplication() vs. getApplicationContext()

Very interesting question. I think it’s mainly a semantic meaning, and may also be due to historical reasons. Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case (for example, in a specific vendor implementation). So if you want … Read more

Difference between getContext() , getApplicationContext() , getBaseContext() and “this”

View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity. Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just … Read more