Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, how to get package name?

In my application I need to know the name of package name. I have no problem when I want to grab it in activities but i can't take it in other classes. Following code is working in activity but i don't know why it has problem in simple class.

String packageName = getPackageName();

In my class I tried to write this code:

Context context = getApplicationContext();
String packageName = context.getPackageName();

but compiler said getApplicationContext() method is undefined for this class.

How can I take package name within this class?

like image 434
Hesam Avatar asked Dec 27 '11 09:12

Hesam


People also ask

How do I find my Android package name?

One method to look up an app's package name is to find the app in the Google Play app store using a web browser. The package name will be listed at the end of the URL after the '? id='.

How do I find my app package name?

You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com.

Is application ID and package name same?

Every Android app has a unique application ID that looks like a Java package name, such as com. example. myapp. This ID uniquely identifies your app on the device and in Google Play Store.


1 Answers

The simple, or another way is to pass Context into the helper class constructor:

MyClassConstructor(Context context){

        String packageName = context.getPackageName(); 
}
like image 89
Lumis Avatar answered Sep 23 '22 03:09

Lumis