Sdílet prostřednictvím


KeyChain.GetPrivateKey(Context, String) Method

Definition

Returns the PrivateKey for the requested alias, or null if the alias does not exist or the caller has no permission to access it (see note on exceptions below).

[Android.Runtime.Register("getPrivateKey", "(Landroid/content/Context;Ljava/lang/String;)Ljava/security/PrivateKey;", "")]
public static Java.Security.IPrivateKey? GetPrivateKey (Android.Content.Context context, string alias);
[<Android.Runtime.Register("getPrivateKey", "(Landroid/content/Context;Ljava/lang/String;)Ljava/security/PrivateKey;", "")>]
static member GetPrivateKey : Android.Content.Context * string -> Java.Security.IPrivateKey

Parameters

context
Context
alias
String

The alias of the desired private key, typically returned via KeyChainAliasCallback#alias.

Returns

Attributes

Exceptions

if the alias was valid but there was some problem accessing it.

if called from the main thread.

Remarks

Returns the PrivateKey for the requested alias, or null if the alias does not exist or the caller has no permission to access it (see note on exceptions below).

This method may block while waiting for a connection to another process, and must never be called from the main thread.

As Activity and Service contexts are short-lived and can be destroyed at any time from the main thread, it is safer to rely on a long-lived context such as one returned from Context#getApplicationContext().

If the caller provides a valid alias to which it was not granted access, then the caller must invoke #choosePrivateKeyAlias again to get another valid alias or a grant to access the same alias.

On Android versions prior to Q, when a key associated with the specified alias is unavailable, the method will throw a KeyChainException rather than return null. If the exception's cause (as obtained by calling KeyChainException.getCause()) is a throwable of type IllegalStateException then the caller lacks a grant to access the key and certificates associated with this alias.

Java documentation for android.security.KeyChain.getPrivateKey(android.content.Context, java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to