Fetch list of all apps on Android device (esp. Android TV)

459 views Asked by At

I'm trying to fetch a list of all launchable apps on an Android TV (Nexus Player) running 5.1.1.

private static Set<ResolveInfo> getInstalledAppsFrom(PackageManager packageManager) {
    Set<ResolveInfo> activities = new HashSet<>();
    activities.addAll(launcherActivitiesIn(packageManager));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activities.addAll(leanbackActivitiesIn(packageManager));
    }
    return activities;
}

private static List<ResolveInfo> launcherActivitiesIn(PackageManager packageManager) {
    Intent intent = new Intent()
            .setAction(Intent.ACTION_MAIN)
            .addCategory(Intent.CATEGORY_LAUNCHER);
    return getResolveInfos(packageManager, intent);
}

private static List<ResolveInfo> getResolveInfos(PackageManager packageManager, Intent intent) {
    return packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static List<ResolveInfo> leanbackActivitiesIn(PackageManager packageManager) {
    Intent intent = new Intent()
            .setAction(Intent.ACTION_MAIN)
            .addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
    return getResolveInfos(packageManager, intent);
}

Using the above, I can get a list of apps, but it doesn't look like the complete list: Netflix is installed on the device but doesn't display, and obviously Settings is missing.

Running the same code on an Android phone running 5.1.1 will include the Settings app.

Is there something about the CATEGORY_LAUNCHER and/or CATEGORY_LEANBACK_LAUNCHER that might prevent some apps showing up?

I also tried:

packageManager.getInstalledApplications(PackageManager.PERMISSION_GRANTED);

to the same effect.

The full code (at the point of writing this question) can be checked out on Github here, where the snippets are from the AppsRepository class.

1

There are 1 answers

0
ataulm On BEST ANSWER

The question did not contain enough detail to answer correctly. The Set<ResolveInfo> was correct - it contained an entry for Netflix.

The issue came from trying to use them:

Intent launchIntent = packageManager.getLaunchIntentForPackage(applicationInfo.packageName);

Here, launchIntent can be null, even if the package name is correct. There is another call that may return the correct Intent:

Intent launchIntent = packageManager.getLeanbackLaunchIntentForPackage(applicationInfo.packageName);

Still though, could not find the intent to launch system Settings. Only Google Settings which displayed as Play Services. Will change accepted answer if someone can solve this!