21

I want to get all application which appears in the menu screen. But, now I only get the user installed apps or all the application (included the system application).

My current code is:

    final PackageManager pm = getActivity().getPackageManager();
    List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_META_DATA);

    ArrayList<PackageInfo> aux = new ArrayList<PackageInfo>();

    for (int i = 0; i < apps.size(); i++) {
        if (apps.get(i).versionName != null && ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1)) {
            aux.add(apps.get(i)); 
        }

With this code, I can get the user installed apps, and if I comment the 'if' instruction, I will get the system apps.

So, I want to get the user installed apps and apps like contacts, gallery and so on.

UPDATE:

    final PackageManager pm = getActivity().getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
beni
  • 3,019
  • 5
  • 35
  • 55
  • 2
    but contacts is a system app? Please explain the difference. What list of apps do you want? – Blundell Jul 06 '13 at 15:10
  • I want to get a list with all the apps which appears in the menu screen. – beni Jul 06 '13 at 15:14
  • but contacts appears in the menu screen? i.e. system apps. Maybe another question will help. What specific apps *don't* you want? – Blundell Jul 06 '13 at 15:17
  • yeah, but now with this code I can't query the list with the apps which appers in the menu. And, I dont know how. – beni Jul 06 '13 at 15:22
  • 2
    I think you need to use PackageManager to find out all the apps that support intent-filter with action "android.intent.action.MAIN" and category "android.intent.category.LAUNCHER" – Wand Maker Jul 06 '13 at 15:23
  • @beni You should write your update as an answer instead of putting it in the question. Then you can accept it to close out the question. As it is, it's still showing in the 'unanswered' category. – Geobits Jul 11 '13 at 19:37

4 Answers4

45
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

Using PackageInfo:

private boolean isSystemPackage(PackageInfo packageInfo) {
    return ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

Using ResolveInfo:

private boolean isSystemPackage(ResolveInfo resolveInfo) {
    return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

Using ApplicationInfo:

private boolean isSystemPackage(ApplicationInfo applicationInfo) {
    return ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

This filters the system package. See this question. Credits: Nelson Ramirez and Kenneth Evans.

Community
  • 1
  • 1
Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
7
final PackageManager pm = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
beni
  • 3,019
  • 5
  • 35
  • 55
  • 3
    what does the "GET_META_DATA" do? I can see the field of the "metadata" in ApplicationInfo objects getting filled, but what is its purpose, and why should we get it? – android developer Apr 17 '14 at 23:41
  • For devices with many applications installed this piece of code is going to throw TransactionTooLargeException, please check this answer http://stackoverflow.com/a/24265790/1245275 – koleanu Jan 29 '15 at 11:29
  • @beni , here how can i skip applications with duplicate package name? – Manu Apr 17 '15 at 09:44
5

I had the same requirement. Ultimately I added another condition to filter down the app list. I just checked whether the app has 'launcher intent'.

So, the resultant code looks like...

PackageManager pm = getPackageManager();
        List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_GIDS);

        for (ApplicationInfo app : apps) {
            if(pm.getLaunchIntentForPackage(app.packageName) != null) {
                // apps with launcher intent
                if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
                    // updated system apps

                } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                    // system apps

                } else {
                    // user installed apps

                }
                appsList.add(app);
            }

        }
joecizac
  • 1,077
  • 2
  • 13
  • 14
0

Below code will give you all the system apps related to Operating System. This will not include Apps like YouTube, Gmail, etc


try {
    ApplicationInfo ai =  getPackageManager().getApplicationInfo(item.packageName, 0);
    if ((ai.flags & ApplicationInfo.FLAG_SYSTEM ) != 0) {
      if((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP ) == 0) {
        appItem.setSystemApp(true);
        Mylog.v("System Apps " + appItem.getAppName());
      }
    }
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
Abhishek Kumar
  • 236
  • 1
  • 10