getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES) 返回 null

发布于 2024-12-03 20:02:40 字数 1500 浏览 3 评论 0 原文

如果我打电话,

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

我会得到已安装软件包的列表,包括它们声明的任何提供程序(即 pis[i].providers 可能为非空)。

但是,如果我在标志中包含 PackageManager.GET_ACITIVITIES,就像

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

我希望获得已安装软件包的“相同”列表,但 pis[i].activities 也为非空。但我没有,我得到一个空列表。

文档

我当前的工作是将 PackageManager.GET_ACTIVITIES 排除在标志之外,然后按如下方式循环返回的列表:

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

但这似乎是一个真正的拼凑。

我唯一能找到的 getInstalledPackages (PackageManager.GET_ACTIVITIES) 返回空列表的提及是这里,但这种情况下的问题似乎是在主应用程序线程之外调用 getInstalledPackages() ,而我的情况并非如此。

ps 这是 Gingerbread 的 .602 VZW 版本,以防万一

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-12-10 20:02:40

我遇到了同样的问题,并找到了更好的解决方法:

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

请注意,我需要查询 PackageManager 两次。一旦使用 getPackageManager().getInstalledPackages(...) 并再次使用 getPackageManager().getPackageInfo(...)

我希望它有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文