getPackageManager ().getInstalledPackages (PackageManager.GET_ACTIVITIES) returns null

五迷三道 提交于 2019-12-31 17:07:23

问题


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:


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.



来源:https://stackoverflow.com/questions/7374704/getpackagemanager-getinstalledpackages-packagemanager-get-activities-return

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