語言

KeyChain.GetPrivateKey(Context, String) 方法

定義

回傳請求別名的 , PrivateKey 若別名不存在或呼叫者無權限存取則回傳 空(詳見下方例外說明)。

[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

參數

context
Context
alias
String

所需私鑰的別名通常透過 KeyChainAliasCallback#alias返回。

傳回

屬性

例外狀況

如果別名是有效的,但存取時有問題。

如果是從主串呼叫的話。

備註

回傳請求別名的 , PrivateKey 若別名不存在或呼叫者無權限存取則回傳 空(詳見下方例外說明)。

此方法可在等待連接至其他程序時阻塞,且絕不可從主執行緒呼叫。

由於 Activity 上下文 Service 壽命短暫且可隨時從主執行緒中刪除,較安全的做法是依賴長壽命上下文,例如從 Context#getApplicationContext()返回的上下文。

如果呼叫者提供了一個有效的別名,但未被授權使用,呼叫者必須再次呼叫 #choosePrivateKeyAlias 以取得另一個有效別名或取得存取該別名的權限。

在 Q 之前的 Android 版本中,當與指定別名相關的金鑰不可用時,該方法會拋出 a KeyChainException 而非回傳 null。 若例外原因(透過呼叫 KeyChainException.getCause()取得)是 類型的 IllegalStateException 拋擲,則呼叫者將無法存取與此別名相關的金鑰與憑證。

Java 文件 android.security.KeyChain.getPrivateKey(android.content.Context, java.lang.String)。

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

適用於