amhed
amhed

Reputation: 3659

Retrieving installed certificates on Android Keychain

I have a x509 certificate that I installed using this code:

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);

The code successfully calls the android UI to install a certificate and forces the user to set a PIN to protect the device. That works fine.

But when I try to access the keychain like this:

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

I get the following error: Java.Lang.IllegalStateException: calling this from your main thread can lead to deadlock

What is the correct way to access the keychain?

Upvotes: 1

Views: 2558

Answers (1)

Snicolas
Snicolas

Reputation: 38168

Android is really great. It's telling you "please, don't do that this way : you may block the user interface". Android UI (display and user events) are handled by a thread in Android : the UI Thread a.k.a the main thread.

If you do something that may take time like a network call, or writing to a file, then you may block the UI and the app will look unresponsive to your users, and it can even stop working by provoking an ANR (App Not Responding).

So the answer is simple, do it in a different thread. On Android, AsyncTask are meant to help you achieve this kind of things fairly easily.

Upvotes: 3

Related Questions