Google登录弃用方法疑问及非弃用实现方案咨询
Google登录弃用方法相关问题解答
为什么官方文档仍使用已弃用的方法
- 文档更新存在滞后性:API弃用后,官方文档的迭代需要走内部流程,无法与API弃用完全同步,通常需要一段时间完成内容替换。
- 兼容性考量:仍有大量开发者在维护基于旧SDK或未迁移到AndroidX的项目,保留旧代码示例可以帮助这类开发者快速适配,同时方便从旧API向新API过渡。
- 功能适配进度:部分One Tap或Firebase登录的功能模块可能还在逐步完成新API的适配,旧示例暂时作为过渡方案保留。
非弃用的Google登录实现方法
1. One Tap 登录(基于Activity Result API)
替代原有的startIntentSenderForResult()和onActivityResult(),使用registerForActivityResult处理登录结果:
步骤1:定义Activity Result Launcher
import android.content.IntentSender import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.IntentSenderRequest import androidx.activity.result.contract.ActivityResultContracts import com.google.android.gms.auth.api.identity.BeginSignInRequest import com.google.android.gms.auth.api.identity.Identity import com.google.android.gms.auth.api.identity.SignInCredential import com.google.android.gms.common.api.ApiException private val oneTapResultLauncher: ActivityResultLauncher<IntentSenderRequest> = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> try { val credential = Identity.getSignInClient(this).getSignInCredentialFromIntent(result.data) val idToken = credential.googleIdToken // 此处处理获取到的ID Token,例如传给后端验证用户身份 } catch (e: ApiException) { // 处理登录异常,如用户取消、验证失败等 } }
步骤2:发起One Tap登录请求
private fun startOneTapSignIn() { val signInRequest = BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions( BeginSignInRequest.GoogleIdTokenRequestOptions.builder() .setSupported(true) .setServerClientId(getString(R.string.your_web_client_id)) // 替换为你的Web客户端ID .setFilterByAuthorizedAccounts(false) // 是否只显示已登录过的账号 .build() ) .build() Identity.getSignInClient(this).beginSignIn(signInRequest) .addOnSuccessListener { result -> try { val intentSenderRequest = IntentSenderRequest.Builder(result.pendingIntent.intentSender).build() oneTapResultLauncher.launch(intentSenderRequest) } catch (e: IntentSender.SendIntentException) { // 处理Intent发送异常 } } .addOnFailureListener { // 处理登录请求失败,例如显示传统Google登录按钮作为备选 } }
2. 传统Google Sign-In(基于Activity Result API)
同样使用registerForActivityResult替代旧的回调方式:
步骤1:定义Activity Result Launcher
import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount import com.google.android.gms.auth.api.signin.GoogleSignInOptions import com.google.android.gms.common.api.ApiException private val googleSignInLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) try { val account = task.getResult(ApiException::class.java) val idToken = account.idToken // 处理ID Token或用户信息 } catch (e: ApiException) { // 处理登录异常 } }
步骤2:发起登录请求
private fun startGoogleSignIn() { val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.your_web_client_id)) // 替换为你的Web客户端ID .requestEmail() .build() val googleSignInClient = GoogleSignIn.getClient(this, gso) googleSignInLauncher.launch(googleSignInClient.signInIntent) }
以上两种实现均使用AndroidX推荐的Activity Result API,完全替代了已弃用的startIntentSenderForResult()和onActivityResult()方法,避免了弃用警告,同时符合最新的Android开发规范。
内容的提问来源于stack exchange,提问作者Ali Doran
相关产品推荐
相关产品推荐

