如何通过编程方式使用apk获取开发的组织名称?

时间:2018-05-07 13:28:11

标签: android apk android-applicationinfo

如果有人知道如何通过使用apk在Android中以编程方式获得开发的组织名称,请分享您的建议。仅供参考,我附上图片。

 This image I have marked the WhatsApp organization name, How to get this organization name programmatically by using apk.

1 个答案:

答案 0 :(得分:1)

没有直接的方法可以从设置中读取此信息。但是,您可以从APK的签名证书中读取此信息,如下所示:

 public String getOrganizationName(final String packageName) {
        String orgName = null;
        try {
            Signature signatures[] = getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures;                
            for (Signature signature : signatures) {
                final byte[] rawCert = signature.toByteArray();
                InputStream certStream = new ByteArrayInputStream(rawCert);

                try {
                    CertificateFactory certFactory = CertificateFactory.getInstance("X509");
                    X509Certificate x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);
                    String issuerDn = x509Cert.getIssuerDN().getName();
                    int indexOfOrg = issuerDn.indexOf("O=");
                    int indexOfSeperator = issuerDn.indexOf(",", indexOfOrg);
                    final String organizationName = issuerDn.substring(indexOfOrg+2, indexOfSeperator);
                    if (orgName == null) {
                        orgName = organizationName;
                    }
                    Log.d("Test", "Organization Name: " + organizationName);

                } catch (Exception e) {
                     e.printStackTrace();
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return orgName;
 }

请按以下方式调用此方法:

getOrganizationName("com.whatsapp"); //You can use any installed package name

它将返回:

WhatsApp Inc.