1

I've got a broadcast receiver that gets "android.intent.action.PACKAGE_ADDED" and "android.intent.action.PACKAGE_REPLACED" intents (so when a package is installed on the phone, my app gets an intent with the package name). But when I try to use the PackageManager to get the PackageInfo by package name, it's throwing a NameNotFoundException.

What reasons might there be for this (other than the package not existing, which obviously isn't the case)? I can't find much on permissions - is there one I'm missing that's causing a misleading error?

try {
    id = UAppIDUtils.GetUAppID(ctx.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES));
} catch (NameNotFoundException e) {
    id = null;
    Log.v(TAG, "Error finding package info");
    e.printStackTrace();
}

ctx is the context; pkgName is the package name.

Michelle
  • 2,830
  • 26
  • 33
  • What does you manifest look like? Try adding the following line to the intent-filter to see if that makes any difference: `` Suggested in [this example code](http://developer.android.com/resources/faq/framework.html#7) and mentioned before [here](http://stackoverflow.com/questions/3510544/android-how-to-intercept-the-install-application-intent). – MH. Nov 05 '11 at 22:51
  • Yep, that's there - I'm getting (at least what looks like) the package name just fine, but then it can't find the name that was just sent to my app as having been installed. Super-stumped on this one. – Michelle Nov 06 '11 at 15:44
  • Well, the only other reasons I can think of is that the package name is missing something - let's not assume that for now - or your call to the PackageManager's `getApplicationInfo()` method is somehow not what it should be - e.g. the flags that the method takes as second parameter. Could you copy in a part of your concerning code? – MH. Nov 07 '11 at 05:43
  • Come to think of it: can you verify that the package is actually installed when you receive the broadcast? Have a look at the PackageManager's `getInstalledPackages()` and `getInstalledApplications()` methods. – MH. Nov 07 '11 at 06:16
  • Printed out a list of installed packages and the package name from the intent is listed. Also added code above. – Michelle Nov 09 '11 at 02:43
  • In that case it probably means that the `NameNotFoundException` gets thrown because of the `GET_SIGNATURES` flag. Try passing in `0`, as several sample codes seem to use that: `ctx.getPackageManager().getPackageInfo(pkgName, 0)`. – MH. Nov 09 '11 at 05:33
  • Tried replacing the GET_SIGNATURES flag with 0 and am still getting the same exception. Going to try splitting the line up a bit, passing in the package name directly from the intent, and maybe checking out getApplicationInfo instead. – Michelle Nov 10 '11 at 18:26

2 Answers2

1

Figured it out - it turns out that the string shipped with the PACKAGE_ADDED intent starts with "package:" and then the package name - I hadn't caught it because it didn't stand out in my print statements as something I hadn't written. Just needed to strip the label off the start of the data string and I was good to go.

Michelle
  • 2,830
  • 26
  • 33
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,258
  • 2
  • 37
  • 43