Last active
January 4, 2020 16:27
-
-
Save comoc/6161522 to your computer and use it in GitHub Desktop.
How to list installed applications on Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//{ Locate follows into your class | |
public static class ApplicationInfo { | |
public String label; | |
public String name; | |
public String packageName; | |
public Drawable icon; | |
@Override | |
public String toString() { | |
return "label:" + label + " name:" + name + " packageName:" | |
+ packageName + " icon: " + icon.toString(); | |
} | |
}; | |
public static List<ApplicationInfo> getApplicationsInfo(Context context) { | |
List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>(); | |
Intent intent = new Intent(Intent.ACTION_MAIN, null); | |
intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
PackageManager pm = context.getPackageManager(); | |
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0); | |
Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm)); | |
for (ResolveInfo ri : activities) { | |
ApplicationInfo info = new ApplicationInfo(); | |
info.packageName = ri.activityInfo.packageName; | |
info.name = ri.activityInfo.name; | |
info.label = (String) ri.loadLabel(pm); | |
info.icon = ri.activityInfo.loadIcon(pm); | |
apps.add(info); | |
} | |
return apps; | |
} | |
//} | |
// Usage | |
class MyActivity extends Activity { | |
void listApps() { | |
List<ApplicationInfo> apps = getApplicationsInfo(this); | |
for (ApplicationInfo info : apps) { | |
// do something... | |
} | |
} | |
public static class ApplicationInfo { | |
public String label; | |
public String name; | |
public String packageName; | |
public Drawable icon; | |
@Override | |
public String toString() { | |
return "label:" + label + " name:" + name + " packageName:" | |
+ packageName + " icon: " + icon.toString(); | |
} | |
}; | |
public static List<ApplicationInfo> getApplicationsInfo(Context context) { | |
List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>(); | |
Intent intent = new Intent(Intent.ACTION_MAIN, null); | |
intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
PackageManager pm = context.getPackageManager(); | |
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0); | |
Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm)); | |
for (ResolveInfo ri : activities) { | |
ApplicationInfo info = new ApplicationInfo(); | |
info.packageName = ri.activityInfo.packageName; | |
info.name = ri.activityInfo.name; | |
info.label = (String) ri.loadLabel(pm); | |
info.icon = ri.activityInfo.loadIcon(pm); | |
apps.add(info); | |
} | |
return apps; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment