对于PackageManager.getInstalledPackages()

时间:2017-01-20 11:46:47

标签: android android-activity null package-managers package-info

尝试检索每个包的所有活动会导致所有这些活动的null ActivityInfo数组:

List<PackageInfo> packageInfos = mPackageManager.getInstalledPackages(
                PackageManager.GET_ACTIVITIES &
                PackageManager.GET_SERVICES &
                PackageManager.GET_RECEIVERS &
                PackageManager.GET_PROVIDERS &
                PackageManager.GET_PERMISSIONS);

for (PackageInfo p : packageInfos) 
    Log.wtf(TAG, p.packageName + ": " + (p.activities == null ? "null" :"not null"));
所有包的

p.activities 为空。根据{{​​3}},它不应该为null,因为设置了GET_ACTIVITIES标志。

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

我的不好,我和按位运算符搞混了。必须使用OR运算符(|)而不是AND运算符(&),如下所示:

List<PackageInfo> packageInfos = mPackageManager.getInstalledPackages(
                PackageManager.GET_ACTIVITIES |
                PackageManager.GET_SERVICES |
                PackageManager.GET_RECEIVERS |
                PackageManager.GET_PROVIDERS |
                PackageManager.GET_PERMISSIONS);

但是,此调用会产生TransactionTooLargeException,因为Android中不同进程之间的数据大小限制已更改,因此大多数probaly返回的packageInfos列表将为null或为空