6

I'm trying to display versionName using below code

PackageManager pkm = getPackageManager();
PackageInfo pki = pkm.getPackageInfo("com.example", PackageManager.GET_CONFIGURATIONS);
String tempCurrentVersion = pki.versionName;

But I'm getting exception at PackageManager.GET_CONFIGURATIONS as NameNotFoundException

Please tell me how i can solve this issue.

Thanks

Richard Ev
  • 51,030
  • 56
  • 187
  • 275
praveenb
  • 10,241
  • 14
  • 57
  • 81

3 Answers3

6

Looks like it is necessary to use try-catch, to prevent NameNotFoundException error. This solution works for me: PackageInfo versionCode and versionName null on phone, but works on emulator .

Community
  • 1
  • 1
joshas
  • 1,416
  • 23
  • 38
1

I use this is my app and it works correctly.

String mVersion = this.getPackageManager().getPackageInfo(
    "com.safeneighborhood", 0).versionName;

If that doesn't work make sure your package name is correct. There aren't any special permissions for this.

Robby Pond
  • 72,024
  • 16
  • 123
  • 118
  • 3
    I get a compile-time error of `Unhandled exception type PackageManager.NameNotFoundException` using this approach. – Richard Ev Sep 03 '11 at 10:45
0

One really annoying time that I ran into this error was because the package name for the app that I was trying to find/intent over to had hidden characters in the text that I copied. Just to be careful retype out your package name or do a log to see what package name you are really looking for as it might not be what you think.

Chris Klingler
  • 5,168
  • 2
  • 36
  • 43