2

I need to know the list of installed applications that can be launched by the user (for example: Browser, Email, Maps, etc.). I read this question about the getInstalledApplications method, so I wrote the following code:

final PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo app : apps) {
    Intent launchIntent = pm.getLaunchIntentForPackage(app.packageName);
if (launchIntent != null) {
        Log.d(LOG_TAG, "getApplicationLabel: " + pm.getApplicationLabel(app));
        Log.d(LOG_TAG, "loadLabel: " + app.loadLabel(pm));
        Log.d(LOG_TAG, "packageName: " + app.packageName);
        Log.d(LOG_TAG, "name: " + app.name);
    }
}

In this way I get the list of applications that can be launched. Each of these applications is characterized by a package name, so if I want to start one of these, just get the launch intent for the package by specifying the package name. This means that each package has at most an activity that can be launched, so each of the applications (which are returned by the getInstalledApplications method) should have a unique package name. Is that correct?

Community
  • 1
  • 1
enzom83
  • 8,080
  • 10
  • 68
  • 114

1 Answers1

2

Android won't typically let you install more than one application using the same package name. In my experience, the .apk file for the second app won't install, let alone run. So, no, you're not going to get more than one application per package name.

It IS possible to have multiple activities be launched via intents from the same application, though. Your code won't get them, because getLaunchIntentForPackage() only returns one intent, but each activity can have its own intent filters. The "Note Pad Example" at http://developer.android.com/guide/topics/intents/intents-filters.html has three different activities, each of which can be launched from the outside.

mjfgates
  • 3,351
  • 1
  • 18
  • 15
  • Also consider Google's maps application -- you can start it either as "Maps" or as "Navigation". – Andrew Aylett Apr 25 '12 at 16:36
  • @AndrewAylett: However the `getLaunchIntentForPackage` method returns only one intent. – enzom83 Apr 25 '12 at 16:40
  • 1
    The obvious question that all this brings up is, "How do you get the set of ALL launch intents for a given app?" Which, I dunno the answer to that one. – mjfgates Apr 25 '12 at 16:42
  • @mjfgates: I read the "Note Pad Example": it has three different activities, but the first one is the only one with a filter with `"android.intent.action.MAIN"` and `"android.intent.category.LAUNCHER"`. – enzom83 Apr 25 '12 at 16:45
  • I need only launch intents with filter with `"android.intent.action.MAIN"` and `"android.intent.category.LAUNCHER"`... – enzom83 Apr 25 '12 at 17:02