Loading...
Loading...
Compare original and translation side by side
androidx.credentialsandroidx.credentialsBackupAgent.onRestoreFinished()Activity.onCreate()allowBackupallowBackupallowBackupBackupAgent.onRestoreFinished()Activity.onCreate()allowBackupallowBackupallowBackupAndroidManifest.xmlisCloudBackupEnabledE2eeUnavailableExceptionisCloudBackupEnabledfalseBackupAgentandroid.app.backup.BackupAgentallowBackupclearCredentialState()BackupAgent.onRestoreFinished()onRestoreFinished()runBlockingBackupAgentBackupAgentActivityallowBackupAndroidManifest.xmlallowBackupBackupAgentallowBackupfalseAndroidManifest.xmlGetCredentialResponsePublicKeyCredentialRestoreCredentialGetRestoreCredentialOptionCredentialOptionGetCredentialRequestCredentialManagerclearCredentialStateClearCredentialStateRequestTYPE_CLEAR_RESTORE_CREDENTIALclearCredentialState()ClearCredentialStateRequestAndroidManifest.xmlisCloudBackupEnabledE2eeUnavailableExceptionisCloudBackupEnabledfalseBackupAgentandroid.app.backup.BackupAgentclearCredentialState()BackupAgentBackupAgent.onRestoreFinished()onRestoreFinished()runBlockingBackupAgentBackupAgentActivityAndroidManifest.xmlallowBackupallowBackupAndroidManifest.xmlfalseBackupAgentGetCredentialResponsePublicKeyCredentialRestoreCredentialGetCredentialRequestGetRestoreCredentialOptionCredentialOptionclearCredentialStateTYPE_CLEAR_RESTORE_CREDENTIALClearCredentialStateRequestclearCredentialState()ClearCredentialStateRequestandroidx.credentialsandroidx.credentials[!NOTE] Note: While the server-side implementation is the same for passkeys and restore keys, your client-side app can support restore keys without supporting passkeys. Because restore keys work independently of the authentication method in your app (for example, passwords or Sign in with Google), you don't need to make any additional changes to the existing authentication methods in your app's code.
[!NOTE] 注意: 虽然通行密钥和恢复密钥的服务端实现相同,但客户端应用可以在不支持通行密钥的情况下支持恢复密钥。由于恢复密钥独立于应用中的认证方式(例如密码或Google登录),你无需对应用代码中现有的认证方式进行任何额外修改。
build.gradlebuild.gradledependencies {
implementation("androidx.credentials:credentials:1.7.0-alpha03")
implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha03")
}dependencies {
implementation("androidx.credentials:credentials:1.7.0-alpha03")
implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha03")
}dependencies {
implementation "androidx.credentials:credentials:1.7.0-alpha03"
implementation "androidx.credentials:credentials-play-services-auth:1.7.0-alpha03"
}[!NOTE] Note: The Restore Credentials feature works regardless of whetheris set in theallowBackup.manifest
dependencies {
implementation "androidx.credentials:credentials:1.7.0-alpha03"
implementation "androidx.credentials:credentials-play-services-auth:1.7.0-alpha03"
}[!NOTE] 注意: Restore Credentials功能的可用性与清单文件中是否设置无关。allowBackup
CredentialManagerCredentialManageronCreateActivitybooleanhas_synced_restore_credential[!NOTE] Note: A restore key is tied to an application's unique package name. If your organization's main app and sub-apps have different package names, create a separate restore key for each app.
ActivityonCreatehas_synced_restore_credential[!NOTE] 注意: 恢复密钥与应用的唯一包名绑定。如果组织的主应用和子应用具有不同的包名,需为每个应用创建独立的恢复密钥。
CredentialManager// Use your app or activity context to instantiate a client instance of
// CredentialManager.
private val credentialManager = CredentialManager.create(context)CredentialManager// 使用应用或活动上下文实例化CredentialManager的客户端实例
private val credentialManager = CredentialManager.create(context)CreateRestoreCredentialRequestcreateCredential()CredentialManager// createRestoreRequest contains the details sent by the server
val response = credentialManager.createCredential(context, createRestoreRequest)CreateRestoreCredentialRequestCredentialManagercreateCredential()// createRestoreRequest包含服务器发送的详细信息
val response = credentialManager.createCredential(context, createRestoreRequest)CreateRestoreCredentialRequestrequestJsonPublicKeyCredentialCreationOptionsJSONisCloudBackupEnabledBooleantruetruefalse[!CAUTION] Caution: It is recommended to settoisCloudBackupEnabled. If cloud backup is disabled and the user restores from a cloud backup, the call to retrieve the restore key fails. Users who restore your app with a cloud backup don't receive the restore key and are not automatically signed in.true
CreateRestoreCredentialRequestisCloudBackupEnabledtruetruefalse[!CAUTION] 警告: 建议将设为isCloudBackupEnabled。如果云端备份被禁用,且用户从云端恢复应用,获取恢复密钥的调用将失败。通过云端恢复应用的用户将无法获取恢复密钥,也无法自动登录。true
CreateRestoreCredentialResponseCreateRestoreCredentialDomExceptionrequestJsonPublicKeyCredentialCreationOptionsJSONE2eeUnavailableExceptionisCloudBackupEnabledtrueE2eeUnavailableExceptioncreateCredentialisCloudBackupEnabledtrueE2eeUnavailableExceptioncreateCredentialisCloudBackupEnabledfalseIllegalArgumentExceptioncreateRestoreRequestuser.idCreateRestoreCredentialResponseCreateRestoreCredentialDomExceptionrequestJsonPublicKeyCredentialCreationOptionsJSONE2eeUnavailableExceptionisCloudBackupEnabledtrueE2eeUnavailableExceptionisCloudBackupEnabledtruecreateCredentialE2eeUnavailableExceptionisCloudBackupEnabledfalsecreateCredentialIllegalArgumentExceptioncreateRestoreRequestuser.idgetCredential()CredentialManagerBackupAgentgetCredentialonRestoreFinishedonRestoreonRestoreFinished[!IMPORTANT] Important: Notifications aren't automatically restored after the restore credentials are retrieved. If you use Firebase to handle notifications, you must fetch and send the Firebase Cloud Messaging (FCM) token to the backend to successfully resume background messaging and notifications.
// Fetch the options required to get the restore key
val authenticationJson = fetchAuthenticationJson()
// Create the GetRestoreCredentialRequest object
val options = GetRestoreCredentialOption(authenticationJson)
val getRequest = GetCredentialRequest(listOf(options))
val response = credentialManager.getCredential(context, getRequest)
// Type-check and extract the restore credential
val credential = response.credential as RestoreCredentialGetCredentialResponseRestoreCredentialCredentialManagergetCredential()BackupAgentonRestoreFinishedgetCredentialonRestoreonRestoreFinished[!IMPORTANT] 重要提示: 获取恢复凭证后,通知不会自动恢复。如果使用Firebase处理通知,必须获取并将Firebase Cloud Messaging(FCM)令牌发送到后端,才能成功恢复后台消息和通知。
// 获取获取恢复密钥所需的选项
val authenticationJson = fetchAuthenticationJson()
// 创建GetRestoreCredentialRequest对象
val options = GetRestoreCredentialOption(authenticationJson)
val getRequest = GetCredentialRequest(listOf(options))
val response = credentialManager.getCredential(context, getRequest)
// 类型检查并提取恢复凭证
val credential = response.credential as RestoreCredentialGetCredentialResponseRestoreCredential[!NOTE] Note: Even though restore keys and passkeys use the same underlying server implementation, differentiate between them when saving them in your app server's database. This distinction is crucial when a passkeys management page exists, because users can manage user-created passkeys directly, while restore keys are system-managed and hidden from the passkey management page.
[!NOTE] 注意: 尽管恢复密钥和通行密钥使用相同的底层服务端实现,但在应用服务器的数据库中存储时需区分两者。当存在通行密钥管理页面时,这种区分至关重要,因为用户可以直接管理自己创建的通行密钥,而恢复密钥由系统管理,不会显示在通行密钥管理页面中。
clearCredentialState()clearCredentialState()clearCredentialState()CredentialManager// Create a ClearCredentialStateRequest object
val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)
// When the user logs out, delete the restore key
val response = credentialManager.clearCredentialState(clearRequest)clearCredentialState()clearCredentialState()CredentialManagerclearCredentialState()// 创建ClearCredentialStateRequest对象
val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)
// 用户退出登录时,删除恢复密钥
val response = credentialManager.clearCredentialState(clearRequest)requestJsonPublicKeyCredentialCreationOptionsJSONauthenticationJsonPublicKeyCredentialCreationOptionsJSONPublicKeyCredentialRequestOptionsJSONrequestJsonPublicKeyCredentialCreationOptionsJSONauthenticationJsonPublicKeyCredentialCreationOptionsJSONPublicKeyCredentialRequestOptionsJSON