android.security.KeyChainException:java.lang.IllegalStateException:uid

时间:2012-08-25 12:57:48

标签: android keychain

错误:

android.security.KeyChainException: java.lang.IllegalStateException: uid 10111 doesn't have permission to access the requested alias

代码:

new Thread(new Runnable() {

        public void run() {
            try {
                X509Certificate[] myCertificates=KeyChain.getCertificateChain(MainActivity.this, "ServerCertificate");
                if(myCertificates!=null)
                {
                    System.out.println("myCertificates size "+myCertificates.length);

                    for(int i=0;i<myCertificates.length;i++)
                    {
                        System.out.println("myCertificates i= "+i+"    "+myCertificates[i]);
                    }
                }
            } catch (KeyChainException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();

1 个答案:

答案 0 :(得分:6)

KeyChain类要求应用程序在安装应用程序后至少调用choosePrivateKeyAlias()一次,然后再调用getPrivateKey()getCertificateChain()。因此,即使事先知道别名,也必须至少调用一次choosePrivateKeyAlias(),否则应用程序与KeyChain使用的内部数据库之间不存在信任。