在Android Keychain上检索已安装的证书

时间:2013-06-16 22:57:47

标签: android xamarin.android xamarin keychain

我使用此代码安装了x509证书:

var certificate = AssetManagement.GetCertificate (xdoc); //this is a helper class that retrieves the certificate

//Code to install to keychain
Intent intent = KeyChain.CreateInstallIntent ();
intent.PutExtra (KeyChain.ExtraCertificate, certificate.GetRawCertData());
intent.PutExtra (KeyChain.ExtraName, "AzureManagement");

StartActivity (intent);

代码成功调用android UI来安装证书,并强制用户设置PIN以保护设备。这很好。

但是当我尝试像这样访问钥匙串时:

var chain = KeyChain.GetCertificateChain (this, "My cert alias");

我收到以下错误:Java.Lang.IllegalStateException: calling this from your main thread can lead to deadlock

访问钥匙串的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

Android非常棒。它告诉你“请,不要这样做:你可能会阻止用户界面”。 Android UI(显示和用户事件)由Android中的线程处理:UI Thread a.k.a是主线程。

如果您做某些事情可能需要花费时间,如网络电话或写入文件,那么您可能会阻止用户界面,应用程序看起来对您的用户没有反应,它甚至可以通过激发ANR(应用程序)停止工作没有回应)。

所以答案很简单,在另一个线程中做。在Android上,AsyncTask旨在帮助您轻松实现此类目标。

相关问题