如要使用 Credential Manager 在自動填入結果中顯示憑證,請使用標準實作建構 GetCredentialRequest,然後將其設為相關檢視畫面。無論回應來自 getCredential API 呼叫或 PendingGetCredentialRequest,處理方式都相同,如下列範例所示。
首先,請建構 GetCredentialRequest:
// Retrieves the user's saved password for your app.valgetPasswordOption=GetPasswordOption()// Get a passkey from the user's public key credential provider.valgetPublicKeyCredentialOption=GetPublicKeyCredentialOption(requestJson=requestJson)valgetCredRequest=GetCredentialRequest(listOf(getPasswordOption,getPublicKeyCredentialOption))
接著,呼叫 getCredential API。系統會顯示「憑證管理工具」選取器。
coroutineScope{try{valresult=credentialManager.getCredential(context=activityContext,// Use an activity-based context.request=getCredRequest)handleSignIn(result)}catch(e:GetCredentialException){handleFailure(e)}}
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["Starting with Android 15 Beta 2, paired with\n[androidx.credentials:1.5.0-alpha01](/jetpack/androidx/releases/credentials#version_15_2), developers can link specific views like\nusername or password fields with Credential Manager requests. When the user\nfocuses on one of these views, the corresponding request is sent to Credential\nManager. The resulting credentials are aggregated across providers and displayed\nin autofill UIs, such as keyboard inline suggestions, or drop-down suggestions.\nThis feature can be used as a fallback when users accidentally dismiss the\nCredential Manager account selector and then tap on the relevant fields.\n\nThe ***Jetpack androidx.credentials*** library is the preferred endpoint for\ndevelopers to use for this feature.\n\n\u003cbr /\u003e\n\n\n**Figure 1:** Autofill results with credentials using password, passkey, and\nSign in with Google.\n| **Note:** This feature is only available for `View` objects at this time.\n\nImplementation\n\nTo use Credential Manager to show credentials in autofill results, use the\n[standard implementation](/identity/sign-in/credential-manager) to build a `GetCredentialRequest` and then set it\nto the relevant views. The response handling is the same, whether the response\ncomes from the `getCredential` API call or the `PendingGetCredentialRequest`, as\nshown in the following example.\n\nFirst, construct a `GetCredentialRequest`: \n\n // Retrieves the user's saved password for your app.\n val getPasswordOption = GetPasswordOption()\n\n // Get a passkey from the user's public key credential provider.\n val getPublicKeyCredentialOption = GetPublicKeyCredentialOption(\n requestJson = requestJson\n )\n\n val getCredRequest = GetCredentialRequest(\n listOf(getPasswordOption, getPublicKeyCredentialOption)\n )\n\nNext, call the `getCredential` API. This displays the Credential Manager\nselector. \n\n coroutineScope {\n try {\n val result = credentialManager.getCredential(\n context = activityContext, // Use an activity-based context.\n request = getCredRequest\n )\n handleSignIn(result)\n } catch (e: GetCredentialException) {\n handleFailure(e)\n }\n }\n\nFinally, enable the autofill experience. Set the `getCredRequest` to relevant\nviews (such as `username, password`) to enable credential results in autofill\nwhen the user interacts with these views.\n**Note:** The `setPendingGetCredentialRequest` is an *extension API* in the androidx.credentials library, and is called differently between Kotlin and Java. \n\n usernameEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(\n getCredRequest\n ) { response -\u003e\n handleSignIn(response)\n }\n\n passwordEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(\n getCredRequest\n ) { response -\u003e\n handleSignIn(response)\n }"]]