認証情報マネージャーを使用して自動入力の結果に認証情報を表示するには、標準実装を使用して 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 UTC。"],[],[],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 }"]]