如果使用者選擇不儲存憑證,系統將不會再提示使用者
為應用程式儲存任何帳戶的憑證。如果你打電話
使用者選擇拒絕後,CredentialsClient.save()的結果會獲得
CANCELED 的狀態碼。使用者之後可以前往 Google
應用程式,前往「密碼專用 Smart Lock」部分。使用者必須啟用
為所有帳戶儲存憑證,下次系統就會提示你儲存憑證。
mCredentialsClient.save(credential).addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"SAVE: OK");Toast.makeText(activity,"Credentials saved",Toast.LENGTH_SHORT).show();return;}Exceptione=task.getException();if(einstanceofResolvableApiException){// Try to resolve the save request. This will prompt the user if// the credential is new.ResolvableApiExceptionrae=(ResolvableApiException)e;try{rae.startResolutionForResult(this,RC_SAVE);}catch(IntentSender.SendIntentExceptionexception){// Could not resolve the requestLog.e(TAG,"Failed to send resolution.",exception);Toast.makeText(activity,"Save failed",Toast.LENGTH_SHORT).show();}}else{// Request has no resolutionToast.makeText(activity,"Save failed",Toast.LENGTH_SHORT).show();}}});</pre>
@OverridepublicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);// ...if(requestCode==RC_SAVE){if(resultCode==RESULT_OK){Log.d(TAG,"SAVE: OK");Toast.makeText(this,"Credentials saved",Toast.LENGTH_SHORT).show();}else{Log.e(TAG,"SAVE: Canceled by user");}}// ...}
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eSmart Lock for Passwords is deprecated; migrate to Credential Manager for enhanced security and user experience with passkey, password, and federated identity support.\u003c/p\u003e\n"],["\u003cp\u003eStore user credentials after sign-in, account creation, or password changes to enable automated authentication in your app using Credential Manager.\u003c/p\u003e\n"],["\u003cp\u003eCreate \u003ccode\u003eCredential\u003c/code\u003e objects with user sign-in information, including email, password (securely), account type, and profile details, before saving them with \u003ccode\u003eCredentialsClient.save()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eHandle user confirmation for new credentials using \u003ccode\u003eResolvableApiException\u003c/code\u003e and \u003ccode\u003estartResolutionForResult()\u003c/code\u003e to ensure smooth credential saving.\u003c/p\u003e\n"],["\u003cp\u003eRetrieve stored credentials using \u003ccode\u003eCredentialsClient.request()\u003c/code\u003e for seamless user authentication within your app.\u003c/p\u003e\n"]]],[],null,["# Store a user's credentials\n\n| **Deprecated:** Smart Lock for Passwords is deprecated. To ensure the continued security and usability of your app, [migrate to\n| Credential Manager](https://developer.android.com/training/sign-in/passkeys/) today. Credential Manager supports passkey, password, and federated identity authentication (such as Sign-in with Google), stronger security, and a more consistent user experience.\n\nAfter users successfully sign in, create accounts, or change passwords, allow\nthem to store their credentials to automate future authentication in your app.\n\nBefore you begin\n----------------\n\n[Configure an Android Studio project](/identity/smartlock-passwords/android/get-started).\n\nStore credentials\n-----------------\n\nCreate a `Credential` object containing a user's sign-in information. For\nexample, to let users store their credentials after successfully signing in with\ntheir passwords: \n\n Credential credential = new Credential.Builder(email)\n .setPassword(password) // Important: only store passwords in this field.\n // Android autofill uses this value to complete\n // sign-in forms, so repurposing this field will\n // likely cause errors.\n .build();\n\nOr, for example, after users successfully\n[sign in with their Google account](/identity/sign-in/android/people): \n\n GoogleSignInAccount gsa = signInTask.getResult();\n Credential credential = new Credential.Builder(gsa.getEmail())\n .setAccountType(IdentityProviders.GOOGLE)\n .setName(gsa.getDisplayName())\n .setProfilePictureUri(gsa.getPhotoUrl())\n .build();\n\nThen, call [`CredentialsClient.save()`](/android/reference/com/google/android/gms/auth/api/credentials/CredentialsClient#save(com.google.android.gms.auth.api.credentials.Credential)) to save users'\ncredentials. If the call to `CredentialsClient.save()` is not immediately\nsuccessful, the credentials might be new, in which case the user must confirm\nthe save request. Resolve the `ResolvableApiException` with\n`startResolutionForResult()` to prompt the user for confirmation.\n\nIf the user chooses not to save credentials, the user won't be prompted again to\nsave any account's credentials for the app. If you call\n`CredentialsClient.save()` after a user has opted out, its result will have a\nstatus code of [`CANCELED`](/android/reference/com/google/android/gms/common/api/CommonStatusCodes#CANCELED). The user can opt in later from the Google\nSettings app, in the Smart Lock for Passwords section. The user must enable\ncredential saving for all accounts to be prompted to save credentials next time. \n\n mCredentialsClient.save(credential).addOnCompleteListener(\n new OnCompleteListener\u003cVoid\u003e() {\n @Override\n public void onComplete(@NonNull Task\u003cVoid\u003e task) {\n if (task.isSuccessful()) {\n Log.d(TAG, \"SAVE: OK\");\n Toast.makeText(activity, \"Credentials saved\", Toast.LENGTH_SHORT).show();\n return;\n }\n\n Exception e = task.getException();\n if (e instanceof ResolvableApiException) {\n // Try to resolve the save request. This will prompt the user if\n // the credential is new.\n ResolvableApiException rae = (ResolvableApiException) e;\n try {\n rae.startResolutionForResult(this, RC_SAVE);\n } catch (IntentSender.SendIntentException exception) {\n // Could not resolve the request\n Log.e(TAG, \"Failed to send resolution.\", exception);\n Toast.makeText(activity, \"Save failed\", Toast.LENGTH_SHORT).show();\n }\n } else {\n // Request has no resolution\n Toast.makeText(activity, \"Save failed\", Toast.LENGTH_SHORT).show();\n }\n }\n });\u003c/pre\u003e\n\n @Override\n public void onActivityResult(int requestCode, int resultCode, Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n\n // ...\n\n if (requestCode == RC_SAVE) {\n if (resultCode == RESULT_OK) {\n Log.d(TAG, \"SAVE: OK\");\n Toast.makeText(this, \"Credentials saved\", Toast.LENGTH_SHORT).show();\n } else {\n Log.e(TAG, \"SAVE: Canceled by user\");\n }\n }\n\n // ...\n\n }\n\nAfter storing credentials, retrieve them by calling\n[`CredentialsClient.request()`](/android/reference/com/google/android/gms/auth/api/credentials/CredentialsClient#request(com.google.android.gms.auth.api.credentials.CredentialRequest)).\n\nTargeting Android O and above\n-----------------------------\n\nWhen you save password credentials using Smart Lock on devices running Android O\nor newer, Smart Lock uses the native autofill confirmation dialog over its own\ndialog whenever possible. (Note that credentials saved using Autofill with\nGoogle are bi-directionally shared with Smart Lock for Passwords.)"]]