尝试检索每个包的所有活动会导致所有文件包的 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.所有包的活动均为空。根据 https://developer.android.com/reference/android/content/pm/PackageInfo.html#activities 它不应该为空,因为设置了GET_ACTIVITIES标志。
那么问题出在哪里呢?
我的错,我搞砸了按位运算符。必须使用 OR 运算符 ( |
) 代替 AND 运算符 ( &
),如下所示:
List<PackageInfo> packageInfos = mPackageManager.getInstalledPackages(
PackageManager.GET_ACTIVITIES |
PackageManager.GET_SERVICES |
PackageManager.GET_RECEIVERS |
PackageManager.GET_PROVIDERS |
PackageManager.GET_PERMISSIONS);
但是,由于 Android 中不同进程之间更改的数据大小限制,此调用会导致TransactionTooLargeException
,因此返回的大多数概率packageInfos
列表将为 null 或空