将 Credential Manager 与自动填充功能集成

从 Android 15 Beta 2 开始,搭配使用 androidx.credentials:1.5.0-alpha01 后,开发者可以将用户名或密码字段等特定视图与 Credential Manager 请求相关联。当用户聚焦于其中一个视图时,系统会向 Credential Manager 发送相应请求。系统会汇总来自各个提供商的凭据,并在自动填充界面(例如键盘内嵌建议或下拉菜单建议)中显示这些凭据。如果用户不小心关闭了 Credential Manager 账号选择器,然后点按相关字段,此功能可用作后备。

Jetpack androidx.credentials 库是开发者针对此功能的首选端点。

插图:自动填充结果中的凭据
图 1:使用密码、通行密钥和“使用 Google 账号登录”功能的凭据自动填充结果。

实现

如需使用 Credential Manager 在自动填充结果中显示凭据,请使用标准实现构建 GetCredentialRequest,然后将其设置为相关视图。无论响应来自 getCredential API 调用还是 PendingGetCredentialRequest,响应处理方式都是相同的,如以下示例所示。

首先,构建一个 GetCredentialRequest

Kotlin


// Retrieves the user's saved password for your app.
val getPasswordOption = GetPasswordOption()

// Get a passkey from the user's public key credential provider.
val getPublicKeyCredentialOption = GetPublicKeyCredentialOption(
    requestJson = requestJson
)

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption, getPublicKeyCredentialOption)
)

Java


// Retrieves the user's saved password for your app.
GetPasswordOption getPasswordOption = new GetPasswordOption();

// Get a passkey from the user's public key credential provider.
GetPublicKeyCredentialOption getPublicKeyCredentialOption =
    new GetPublicKeyCredentialOption(requestJson);

GetCredentialRequest getCredRequest = new GetCredentialRequest(
    Arrays.asList(getPasswordOption, getPublicKeyCredentialOption)
);

接下来,调用 getCredential API。此时会显示 Credential Manager 选择器。

Kotlin


coroutineScope.launch {
    try {
        val result = credentialManager.getCredential(
            context = activityContext, // Use an activity-based context.
            request = getCredRequest
        )
        handleSignIn(result);
    } catch (GetCredentialException e) {
        handleFailure(e);
    }
}

Java


coroutineScope.launch(new CoroutineScopeRunnable() {
    @Override
    public void run(@NonNull CoroutineScope scope) {
        try {
            GetCredentialResponse result = credentialManager.getCredential(
                activityContext, // Use an activity-based context.
                getCredRequest
            );
            handleSignIn(result);
        } catch (GetCredentialException e) {
            handleFailure(e);
        }
    }
});

最后,启用自动填充体验。将 getCredRequest 设置为相关视图(例如 username, password),以便在用户与这些视图互动时在自动填充中启用凭据结果。

Kotlin


import androidx.credentials.PendingGetCredentialRequest

usernameEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
    getCredRequest) { response -> handleSignIn(response)
}

passwordEditText.pendingGetCredentialRequest = PendingGetCredentialRequest(
    getCredRequest) { response -> handleSignIn(response)
}

Java


import androidx.credentials.CredentialManagerViewHandler;
import androidx.credentials.PendingGetCredentialRequest;

CredentialManagerViewHandler.setPendingGetCredentialRequest(
               usernameEditText, new PendingGetCredentialRequest(
                       getCredRequest, result -> {
                           handleSignIn(result);
                           return null;
                       }
               )

CredentialManagerViewHandler.setPendingGetCredentialRequest(
               passwordEditText, new PendingGetCredentialRequest(
                       getCredRequest, result -> {
                           handleSignIn(result);
                           return null;
                       }
               )