ผสานรวมเครื่องมือจัดการข้อมูลเข้าสู่ระบบกับการป้อนข้อความอัตโนมัติ

เริ่มต้นด้วย Android 15 เบต้า 2 (ที่จับคู่กับ androidx.credentials:1.5.0-alpha01 นักพัฒนาซอฟต์แวร์สามารถลิงก์ข้อมูลพร็อพเพอร์ตี้บางรายการได้ เช่น ช่องชื่อผู้ใช้หรือรหัสผ่านที่มีคำขอเครื่องมือจัดการข้อมูลเข้าสู่ระบบ เมื่อผู้ใช้โฟกัสที่มุมมองใดมุมมองหนึ่งเหล่านี้ ระบบจะส่งคําขอที่เกี่ยวข้องไปยังเครื่องมือจัดการข้อมูลเข้าสู่ระบบ ระบบจะรวบรวมข้อมูลเข้าสู่ระบบที่ได้จากผู้ให้บริการต่างๆ และแสดง ใน UI ที่ป้อนอัตโนมัติ เช่น คำแนะนำในบรรทัดของแป้นพิมพ์ หรือคำแนะนำแบบเลื่อนลง ฟีเจอร์นี้สามารถใช้เป็นฟีเจอร์สำรองในกรณีที่ผู้ใช้ปิด ตัวเลือกบัญชีเครื่องมือจัดการข้อมูลเข้าสู่ระบบ แล้วแตะช่องที่เกี่ยวข้อง

ไลบรารี Jetpack androidx.credentials เป็นปลายทางที่แนะนำสำหรับ สำหรับฟีเจอร์นี้ด้วย

ภาพประกอบแสดงข้อมูลเข้าสู่ระบบในผลลัพธ์ของการป้อนข้อความอัตโนมัติ
รูปที่ 1: แสดงผลลัพธ์ในการป้อนข้อความอัตโนมัติด้วยข้อมูลเข้าสู่ระบบโดยใช้รหัสผ่าน พาสคีย์ และ ลงชื่อเข้าใช้ด้วย Google

การใช้งาน

หากต้องการใช้เครื่องมือจัดการข้อมูลเข้าสู่ระบบเพื่อแสดงข้อมูลเข้าสู่ระบบในผลการค้นหาที่ป้อนข้อความอัตโนมัติ ให้ใช้ การใช้งานมาตรฐานเพื่อสร้าง GetCredentialRequest จากนั้นตั้งค่า ไปยังข้อมูลพร็อพเพอร์ตี้ที่เกี่ยวข้อง การจัดการการตอบกลับจะเหมือนกัน ไม่ว่าการตอบกลับ มาจากการเรียก API getCredential หรือ 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 ซึ่งจะแสดงตัวเลือกเครื่องมือจัดการข้อมูลเข้าสู่ระบบ

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;
                       }
               )