views:

49

answers:

2

Hi All This should be simple but I can't find any info on this...

I simply want to read the package value in the android manifest...

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="THIS"

the reason is simple I have to call context.getResources().getIdentifier(...) and I need the package.

since this code will be reused in other apps I want to make it fool proof when I export it and therefore not have to change the value each time.

anybody knows how to do this ?

+1  A: 

From your "main" Activity class:

String package = this.getClass().getPackage().getName();
MatrixFrog
+1  A: 

Within an Activity, you can simply call getPackageName(). If you should happen to need additional data from the manifest, you can use the PackageInfo class: http://developer.android.com/reference/android/content/pm/PackageInfo.html

Example of setting a TextView to your app version:

    try {
        PackageManager pm = getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
        TextView version = (TextView) findViewById(R.id.version);
        version.setText(packageInfo.versionName);
    } catch (NameNotFoundException e) {}
Ian G. Clifton