繁体   English   中英

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

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

我有一个listview ,显示所有已安装的应用程序,包括一些系统应用程序,但不显示图库,联系人,消息应用程序。 请告诉我如何获得所有这些系统应用程序。 这是我的代码

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;
    }

告诉我哪里弄错了。 帮我一些代码。

您要列出的应用程序(图库,消息等)是在系统分区中编写的,因此(app.flags & ApplicationInfo.FLAG_SYSTEM) == 1)将为true。

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

这就是为什么你想要列出的应用程序被跳过的原因。

如果您想获得启动器中列出的应用,即类别为<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);
    }

试试这个代码

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()

编辑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);
           }

这可能对你有所帮助

由于您要显示所有已安装的应用程序。 您可以删除代码中的if-else块,只需添加所有应用程序并显示它们即可。 或者在你的代码中进行这些更改

1)通过此代码获取所有应用程序。

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

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
    }
}

尝试这种不同的方式。

//第一

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

//第二个

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)); 
}

//第三

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