2

First, I would like to say this is my first post on StackOverflow, so I hope I am doing this right.

I am attempting to make an Android App that displays all of a user's apps. The issue that I'm having is that some apps are being shown multiple times. I do not know how to check the activities associated with the package, but I think it is a similar situation to this:

Android: How to get a list of installed activities, as they appear in launcher, without duplicates

The Intent Solution does not display any duplicates, but how would I use something similar to show in a GridView?

EDIT: Thank you for your quick response. This is the way that I already get the packages. I was not using the 0 flag, however. After substituting the 0 flag in, I still received duplicates. Is it something to do with the way GridView's work? I happen to notice that when I scroll down on the grid view and go back up, my list is in a different order.

CODE FOR PULLING THE APPS INTO A LIST:

ArrayList<ApplicationInfo> apps =  (ArrayList<ApplicationInfo>) manager.getInstalledApplications(0);  //manager is a variable containing getPackageManager

EDIT 2:

After using the following code to output the ArrayList:

 for(int i = 0; i < apps.size(); i++){

        Log.i((String) apps.get(i).loadLabel(manager), apps.get(i).packageName);

    }

I am able to conclude that there are no duplicates in the array. However, they somehow appear in the gridview.

FINAL EDIT:

The top answer on this question ended up solving the GridView issue:

Items inside GridView getting repeated when screen scrolls

Community
  • 1
  • 1
Chris Ruskai
  • 359
  • 1
  • 2
  • 14

2 Answers2

2

Here is an alternate method for getting currently installed packages. I don't get duplicates when I grab the info this way:

            PackageManager packageManager = null;
            List<ApplicationInfo> appsList = new ArrayList<ApplicationInfo>();
            try {
                packageManager = getPackageManager();
                if (packageManager != null) {
                    appsList = packageManager.getInstalledApplications(0);
                }
            } catch (Exception e1) {
                Log.v("TAG", "This is my error: ", e1);
            }

You can then retreive a variety of info from the ApplicationInfo object (including icon, name, etc).

Booger
  • 18,579
  • 7
  • 55
  • 72
0

This should resolve the problem once and for all.

     PackageManager packageManager = null;
 List<ApplicationInfo> appsListTest = new ArrayList<ApplicationInfo>();
 try {

     packageManager = context.getPackageManager();
     if (packageManager != null) {
         appsListTest = packageManager.getInstalledApplications(0);

     }
 } catch (Exception e1) {
     Log.v("TAG", "This is my error: ", e1);
 }

 Log.d("LOG", " New PM : " + appsListTest.size() );

 Iterator<ApplicationInfo> iterator = appsListTest.iterator();
while (iterator.hasNext()) {
    Log.d("LOG", " : " + iterator.next().loadLabel(packageManager).toString());



}
drlobo
  • 2,139
  • 5
  • 32
  • 42