getPackageManager ().getInstalledPackages (Package

2019-03-14 02:04发布

If I call

    PackageManager pm = getPackageManager () ;
    List<PackageInfo> pis = pm.getInstalledPackages (PackageManager.GET_PROVIDERS) ;

I get a list of installed packages, including any provivders they declare (i.e, with pis[i].providers possibly being non-null).

However, if I include PackageManager.GET_ACITIVITIES among the flags, as in

    PackageManager pm = getPackageManager () ;
    List<PackageInfo> pis = pm.getInstalledPackages (PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS) ;

I expect to get the "same" list of installed packages, but with pis[i].activities being non-null as well. But I don't, I get an empty list.

Is there something special about including PackageManager.GET_ACTIVITES among the flags that isn't mention in the documentation?

My current work around is to leave PackageManager.GET_ACTIVITIES out of the flags, then loop through the returned list as follows:

    for (PackageInfo pi : pis) {
        try {
            PackageInfo tmp = pm.getPackageInfo (pi.packageName, PackageManager.GET_ACTIVITIES) ;
            pi.activities = tmp.activities ;
            }
        catch (NameNotFoundException e) {
            Log.e (TAG, e.getMessage ()) ;
            }

But that seems like a real kludge.

The only mention I could find of getInstalledPackages (PackageManager.GET_ACTIVITIES) returning an empty list is here, but the problem in that case seems to have been something about calling getInstalledPackages() outside the main application thread and that is not the situation in my case.

p.s. this is the .602 VZW build of Gingerbread, in case that matters

1条回答
祖国的老花朵
2楼-- · 2019-03-14 03:01

I came across the same issue and found out a better workaround:

public void listAllActivities() throws NameNotFoundException
{
    List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
    for(PackageInfo pack : packages)
    {
        ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities;
        Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities");
        if(activityInfo!=null)
        {
            for(int i=0; i<activityInfo.length; i++)
            {
                Log.i("PC", pack.packageName + " ::: " + activityInfo[i].name);
            }
        }
    }
}

Notice that I need to query PackageManager twice. Once using getPackageManager().getInstalledPackages(...) and again using getPackageManager().getPackageInfo(...)

I hope it helps.

查看更多
登录 后发表回答