如需使用 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,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Integrate Credential Manager with autofill\n\nStarting 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--------------\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 }"]]