1

When using the PackageManager to get a list of applications from the device, if i call pm.getApplicationIcon(app); and the application does not have an icon, the system will return the system default as it's supposed to do, but i get this error output in my console:

W/ResourceType: No package identifier when getting value for resource number 0x00000000
W/PackageManager: Failure retrieving resources for com.google.android.gsf.login: Resource ID #0x0

Since i'm looping through all applications on the phone, this turns into some pretty bad spam that I'd like to avoid if possible.

My Code

PackageManager pm = getActivity().getApplicationContext().getPackageManager();

for (ApplicationInfo app : pm.getInstalledApplications(0)) {
    long dataUsed = ((TrafficStats.getUidRxBytes(app.uid) + TrafficStats.getUidTxBytes(app.uid)) / 1000) / 1000;

    if ((int)dataUsed > 0) {
        String name = pm.getApplicationLabel(app).toString();
        Drawable icon = pm.getApplicationIcon(app);

        // Custom object
        DataUsageItem item = new DataUsageItem(name, icon, false, dataUsed, new int[]{ 0 , 0 , 0 , 0 , 0 , 0 , 0 });
        items.add(item);
    }
}
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • IIRC, the `ApplicationInfo` for each should have an `icon` field which is the resource ID. Check if it's nonzero before calling `getApplicationIcon()`. – Mike M. Nov 28 '18 at 08:17
  • @MikeM. is there a way to still get the "Default android application icon" without using this method then? Because I'd still like that. – Nathan F. Nov 29 '18 at 20:59
  • Umm, off the cuff, if that's zero, load `android.R.drawable.sym_def_app_icon` instead. Pretty sure that's the right ID. – Mike M. Nov 29 '18 at 21:06
  • @MikeM. seems to work! If you want to submit it as an answer i'll accept it. – Nathan F. Dec 02 '18 at 23:35
  • Cool. Just FYI, I have double-checked the source code to make sure that's correct, and not only working "by accident". Glad it helped. Cheers! – Mike M. Dec 03 '18 at 16:04

1 Answers1

2

The ApplicationInfo for each will have an icon field that holds the resource ID for that drawable in the given package. Check that it's nonzero before calling getApplicationIcon().

For those packages that do not have an icon – i.e., those whose icon field is zero – you can retrieve the system default icon with the android.R.drawable.sym_def_app_icon ID.

Mike M.
  • 38,532
  • 8
  • 99
  • 95