简体   繁体   English

Android:获取包括系统应用在内的所有已安装应用列表的问题

[英]Android: Issue in getting a list of all installed apps including system apps

I have a listview which show the all installed apps including some system apps but does not show gallery, contact, messages apps. 我有一个listview ,显示所有已安装的应用程序,包括一些系统应用程序,但不显示图库,联系人,消息应用程序。 Please tell me how can I get all these system apps. 请告诉我如何获得所有这些系统应用程序。 Here is my code 这是我的代码

public static List getInstalledApplication(Context c)
    {
       // return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);

        List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
        PackageManager pm = c.getPackageManager();
        List<ApplicationInfo> apps = pm.getInstalledApplications(0);

        for(ApplicationInfo app : apps) {
            //checks for flags; if flagged, check if updated system app
            if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
                installedApps.add(app);
                //it's a system app, not interested
            } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                //Discard this one
                //in this case, it should be a user-installed app
              //  installedApps.add(app);
            } else {
                installedApps.add(app);
            }
        }
        return installedApps;
    }

Tell me where I make mistake. 告诉我哪里弄错了。 Help me with some code. 帮我一些代码。

The apps that you want to list (Gallery, Message, etc) are written in system partition and hence, (app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) will be true. 您要列出的应用程序(图库,消息等)是在系统分区中编写的,因此(app.flags & ApplicationInfo.FLAG_SYSTEM) == 1)将为true。

FLAG_SYSTEM
if set, this application is installed in the device's system image. 

Thats why the apps that you want to also list are getting skipped. 这就是为什么你想要列出的应用程序被跳过的原因。

If you want to get apps which are listed in your launcher ie apps with category <Launcher> get the list using following code 如果您想获得启动器中列出的应用,即类别为<Launcher>应用,请使用以下代码获取列表

    final PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
    // using hashset so that there will be no duplicate packages, 
    // if no duplicate packages then there will be no duplicate apps
    HashSet<ApplicationInfo> installedApps = new HashSet<ApplicationInfo>(0);

    // getting package names and adding them to the hashset
    for (ResolveInfo resolveInfo : resInfos) {
        installedApps.add(resolveInfo.activityInfo.applicationInfo);
    }

Try this code 试试这个代码

final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

 for (ApplicationInfo packageInfo : packages) {
      Log.d(TAG, "Installed package :" + packageInfo.packageName);
      Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
      Log.d(TAG, "Launch Activity :" +  pm.getLaunchIntentForPackage(packageInfo.packageName)); 
  }
 // the getLaunchIntentForPackage returns an intent that you can use with startActivity()

EDIT 1 编辑1

PackageManager pm = getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
 List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
          for (ResolveInfo resolveInfo : lst) {
              Log.d("Test", "New Launcher Found: " + resolveInfo.activityInfo.packageName);
           }

this may helps you 这可能对你有所帮助

Since you want to show all the installed apps. 由于您要显示所有已安装的应用程序。 You could get rid of the if-else block in your code and simply add all the apps and display them. 您可以删除代码中的if-else块,只需添加所有应用程序并显示它们即可。 or make these change in you code 或者在你的代码中进行这些更改

1) Fetch all the apps by this code. 1)通过此代码获取所有应用程序。

List<ApplicationInfo> apps = getPackageManager().getInstalledPackages(0);

2) And separate system apps from user installed with the following code: 2)使用以下代码安装的用户单独的系统应用程序:

List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo app : apps) {
    if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
        // It is a system app
    } else {
        // It is installed by the user
    }
}

Try this different ways. 尝试这种不同的方式。

//First //第一

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = context.getPackageManager().queryIntentActivities(mainIntent, 0);

// Second //第二个

PackageManager pm = getPackageManager();
List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
  Log.d(TAG, "Installed package :" + packageInfo.packageName);
  Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
}

// Third //第三

private List getInstalledComponentList()
        throws NameNotFoundException {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
    List componentList = new ArrayList();
    String name = null;

    for (ResolveInfo ri : ril) {
        if (ri.activityInfo != null) {
            Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
            if (ri.activityInfo.labelRes != 0) {
                name = res.getString(ri.activityInfo.labelRes);
            } else {
                name = ri.activityInfo.applicationInfo.loadLabel(
                        getPackageManager()).toString();
            }
            componentList.add(name);
        }
    }
    return componentList;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM