Począwszy od Androida 15 w wersji beta 2, w połączeniu z biblioteką androidx.credentials:1.5.0-alpha01, deweloperzy mogą łączyć określone widoki, takie jak pola nazwy użytkownika lub hasła, z żądaniami menedżera danych logowania. Gdy użytkownik skupi się na jednym z tych widoków, odpowiednia prośba zostanie wysłana do usługi Credential Manager. Uzyskane w ten sposób dane są agregowane z danych różnych dostawców i wyświetlane w interfejsie autouzupełniania, np. w formie sugestii na klawiaturze lub sugestii w menu. Ta funkcja może być używana jako funkcja zastępcza, gdy użytkownicy przypadkowo zamkną selektor kont w Menedżerze danych logowania, a potem klikną odpowiednie pola.
Biblioteka Jetpack androidx.credentials jest preferowanym punktem końcowym dla programistów, którzy chcą korzystać z tej funkcji.
Rysunek 1. Autouzupełnianie wyników wyszukiwania danymi logowania za pomocą hasła, klucza dostępu i
Zaloguj się przez Google.
Implementacja
Aby używać Menedżera danych logowania do wyświetlania danych logowania w wynikach autouzupełniania, użyj standardowej implementacji, aby utworzyć obiekt GetCredentialRequest
, a następnie przypisz go do odpowiednich widoków. Obsługa odpowiedzi jest taka sama, niezależnie od tego, czy
pochodzi z wywołania interfejsu API getCredential
lub z metody PendingGetCredentialRequest
, jak
w poniższym przykładzie.
Najpierw utwórz 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) );
Następnie wywołaj interfejs API getCredential
. Spowoduje to wyświetlenie menedżera danych logowania.
.
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); } } });
Na koniec włącz autouzupełnianie. Ustaw getCredRequest
jako trafne
widoki danych (takie jak username, password
), aby włączyć wyniki danych logowania w autouzupełnianiu
gdy użytkownik wejdzie w interakcję z tymi wyświetleniami.
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; } )