0

I am following the code given in the below link:

http://xjaphx.wordpress.com/2011/06/12/create-application-launcher-as-a-list/

to list out installed application on my phone.

But the problem here is that it is listing all the applications.

All system related apps also like.

Network Location, Sound Search for Google play, Launcher, Package access helper

How do I filter not to show non-launchable apps?

I tried the following:

Instead of this

  c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA) 

I tried:

  c.getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES)

But same result.

Can somebody help me out with this please?

Thanks!

UPDATE:

Thanks to MH for guiding. Now I am able to see only installed app's from playstore. How to do also get System default apps like Gmail, Clock, settings, Google maps etc.

Here is what I tried:

    List<ApplicationInfo> appInfos = c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
    List<ApplicationInfo> userAppInfos = new ArrayList<ApplicationInfo>();
    List<ApplicationInfo> systemAppInfos = new ArrayList<ApplicationInfo>();
    for (ApplicationInfo info : appInfos) 
    {
        if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) 
        {
            systemAppInfos.add(info);
        }
        else
        {
            userAppInfos.add(info);
        }
    }
    return userAppInfos;
TheDevMan
  • 5,914
  • 12
  • 74
  • 144

2 Answers2

0

What exactly do you mean about 'only specific apps? If you want to filter out non-launchable apps or packages (for example, only want to show apps which displaying in apps screens), you can use queryIntentActivities method in PackageManager w/ a proper intent instead of getInstalledApplications.

    final PackageManager pm = getPackageManager();
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);

    if( list != null ){
        for( ResolveInfo resolveInfo : list ){
            Log.d("Apps", resolveInfo.loadLabel(pm).toString());
        }
    }
Chansuk
  • 792
  • 4
  • 7
  • Thanks for the quick note. Looks like I can't update the code with the code given in the example project. Is this possible, if so How? – TheDevMan Aug 21 '13 at 09:27
  • You can just fix some part of AppInfoAdapter adapter in example. If you check the [ResolveInfo](http://developer.android.com/reference/android/content/pm/ResolveInfo.html) class that I used, there is activityInfo field already there, so you can just use it. – Chansuk Aug 21 '13 at 10:26
0

It sounds like you want to get a list of installed applications but filter out any system apps. If that's indeed the case, you can use the returned ApplicationInfo instances to determine whether the app is consider a system app or a regular, user-installed app.

Suppose we wanted to split the installed applications into two lists: one with system apps and one with downloaded apps. We could do that as follows:

List<ApplicationInfo> appInfos = getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> userAppInfos = new ArrayList<ApplicationInfo>();
List<ApplicationInfo> systemAppInfos = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : appInfos) {
    if (info.flags & ApplicationInfo.FLAG_SYSTEM != 0) systemAppInfos.add(info);
    else userAppInfos.add(info);
}

Note that the ApplicationInfo.FLAG_SYSTEM constant indicates:

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

You may or may not also want to do something with ApplicationInfo.FLAG_UPDATED_SYSTEM_APP:

This is set if this application has been installed as an update to a built-in system application.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks for the quick note..Looks like I can't update the code on the following package shown in this tutorial. http://xjaphx.wordpress.com/2011/06/12/create-application-launcher-as-a-list/ Is it possible, if so how? – TheDevMan Aug 21 '13 at 09:24
  • I'm not sure what you mean. If you want to change the list of displayed apps in the tutorial, just change the list that is passed into the `AppInfoAdapter`. Using the example in my answer: basically the tutorial is now passing in `appInfos`, but if you copy-paste some of the code above, you could pass on `userAppInfos` in stead to display a subset of the full list. – MH. Aug 21 '13 at 09:46
  • How do I do it? The tutorial code: public static List getInstalledApplication(Context c) { return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA); } What would I return in this case with your sample code? can you please help me out with snippet. Thanks! – TheDevMan Aug 21 '13 at 10:34
  • I tried it.. Works. But the problem is that I am not able to get any default applications like Gmail App, Clock, Settings... This is shows only list items that are installed by user. – TheDevMan Aug 21 '13 at 11:56