How to get installed applications in Android and no system apps?

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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 apps = pm.getInstalledPackages(PackageManager.GET_META_DATA);      ArrayList aux = new ArrayList();      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 apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);

回答1:

final PackageManager pm = getPackageManager(); List 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.



回答2:

final PackageManager pm = getActivity().getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);


回答3:

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 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)        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!