0

有没有人在带有java反射的Android源代码中使用隐藏类PackageParser

我可以通过反射方式获取此类及其方法 collectCertificates() ,但我不断获取 NoSuchMethodException 及其方法generatePackageInfo()

我想知道以下句子是否会导致该问题,因为我不太确定int[].class的用法:

partypesGeneratePackageInfo[1] = int[].class;

这是我的代码:

    private PackageInfo parsePackage(String archiveFilePath, int flags)
        throws Exception, Exception {
    // get class:PackageParser
    Class clsPackageParser = Class
            .forName("android.content.pm.PackageParser");

            ......      

    // get method:collectCertificates
    Class partypesCollectCertificates[] = new Class[2];
    partypesCollectCertificates[0] = clsPackage;
    partypesCollectCertificates[1] = int.class;
    Method methCollectCertificates = clsPackageParser.getMethod(
            "collectCertificates", partypesCollectCertificates);

    // invoke method:collectCertificates
    Object arglistMethCollectCertificates[] = new Object[2];
    arglistMethCollectCertificates[0] = objPackage;
    arglistMethCollectCertificates[1] = flags;
    methCollectCertificates.invoke(objPackageParser,
            arglistMethCollectCertificates);

    // get method:generatePackageInfo
    Class partypesGeneratePackageInfo[] = new Class[3];
    partypesGeneratePackageInfo[0] = clsPackage;
    partypesGeneratePackageInfo[1] = int[].class;
    partypesGeneratePackageInfo[2] = int.class;
    Method methGeneratePackageInfo = clsPackageParser.getMethod(
            "generatePackageInfo", partypesGeneratePackageInfo);

    ......
}
4

1 回答 1

0

据我所知,声明generatePackageInfo是:

public static PackageInfo generatePackageInfo(PackageParser.Package p,
        int gids[], int flags, long firstInstallTime, long lastUpdateTime) {

所以这是 5 个参数:PackageParser.Package.class, int[].class, int.class, long.class, long.class.

由于这不是公开的 API,因此参数的数量很可能会随着版本的变化而变化。

于 2012-06-08T03:27:24.897 回答