3

I have a listview which show the all installed apps including some system apps but does not show gallery, contact, messages apps. Please tell me how can I get all these system apps. Here is my code

public static List getInstalledApplication(Context c)
    {
       // return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);

        List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
        PackageManager pm = c.getPackageManager();
        List<ApplicationInfo> apps = pm.getInstalledApplications(0);

        for(ApplicationInfo app : apps) {
            //checks for flags; if flagged, check if updated system app
            if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
                installedApps.add(app);
                //it's a system app, not interested
            } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                //Discard this one
                //in this case, it should be a user-installed app
              //  installedApps.add(app);
            } else {
                installedApps.add(app);
            }
        }
        return installedApps;
    }

Tell me where I make mistake. Help me with some code.

7geeky
  • 438
  • 1
  • 12
  • 26
faisal iqbal
  • 724
  • 1
  • 8
  • 20

3 Answers3

1

The apps that you want to list (Gallery, Message, etc) are written in system partition and hence, (app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) will be true.

FLAG_SYSTEM
if set, this application is installed in the device's system image. 

Thats why the apps that you want to also list are getting skipped.

If you want to get apps which are listed in your launcher i.e apps with category <Launcher> get the list using following code

    final PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
    // using hashset so that there will be no duplicate packages, 
    // if no duplicate packages then there will be no duplicate apps
    HashSet<ApplicationInfo> installedApps = new HashSet<ApplicationInfo>(0);

    // getting package names and adding them to the hashset
    for (ResolveInfo resolveInfo : resInfos) {
        installedApps.add(resolveInfo.activityInfo.applicationInfo);
    }
Micer
  • 8,731
  • 3
  • 79
  • 73
Neji
  • 6,591
  • 5
  • 43
  • 66
0

Since you want to show all the installed apps. You could get rid of the if-else block in your code and simply add all the apps and display them. or make these change in you code

1) Fetch all the apps by this code.

List<ApplicationInfo> apps = getPackageManager().getInstalledPackages(0);

2) And separate system apps from user installed with the following code:

List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo app : apps) {
    if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
        // It is a system app
    } else {
        // It is installed by the user
    }
}
7geeky
  • 438
  • 1
  • 12
  • 26
0

Try this different ways.

//First

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = context.getPackageManager().queryIntentActivities(mainIntent, 0);

// Second

PackageManager pm = getPackageManager();
List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
  Log.d(TAG, "Installed package :" + packageInfo.packageName);
  Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
}

// Third

private List getInstalledComponentList()
        throws NameNotFoundException {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
    List componentList = new ArrayList();
    String name = null;

    for (ResolveInfo ri : ril) {
        if (ri.activityInfo != null) {
            Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
            if (ri.activityInfo.labelRes != 0) {
                name = res.getString(ri.activityInfo.labelRes);
            } else {
                name = ri.activityInfo.applicationInfo.loadLabel(
                        getPackageManager()).toString();
            }
            componentList.add(name);
        }
    }
    return componentList;
 }
Andy Developer
  • 3,071
  • 1
  • 19
  • 39