导入google依赖
implementation 'com.google.android.gms:play-services-auth:20.1.0'
具体信息可以查看google官方文档
google登录的工具类
class GoogleLoginUtil {
private val TAG = "GoogleLoginUtil"
private val RC_SIGN_IN = 9009
private val googleClientId =
"google平台获取"
private var mActivity: Activity;
private var mGoogleSignInClient: GoogleSignInClient? = null;
private var mLoginCallBack: LoginCallBack? = null;
constructor(mActivity: Activity) {
this.mActivity = mActivity
}
interface LoginCallBack {
fun onLoginSuccess()
fun onLoginError(code: Int)
}
fun init() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(googleClientId)
.requestId()
.requestProfile()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(mActivity, gso);
}
fun googleLogin(callBack: LoginCallBack) {
mLoginCallBack = callBack;
val account = GoogleSignIn.getLastSignedInAccount(mActivity)
if (account != null) {
Log.e(TAG, "account==================================\n\n\n")
Log.e(TAG, "account ${account.account}")
Log.e(TAG, "displayName ${account.displayName}")
Log.e(TAG, "email ${account.email}")
Log.e(TAG, "familyName ${account.familyName}")
Log.e(TAG, "givenName ${account.givenName}")
Log.e(TAG, "grantedScopes ${account.grantedScopes}")
Log.e(TAG, "id ${account.id}")
Log.e(TAG, "idToken ${account.idToken}")
Log.e(TAG, "isExpired ${account.isExpired}")
Log.e(TAG, "photoUrl ${account.photoUrl}")
Log.e(TAG, "requestedScopes ${account.requestedScopes}")
Log.e(TAG, "serverAuthCode ${account.serverAuthCode}")
Log.e(TAG, "account==================================\n\n\n")
} else {
mActivity.runOnUiThread {
val signInIntent = mGoogleSignInClient?.signInIntent
mActivity.startActivityForResult(signInIntent, RC_SIGN_IN)
}
}
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode === RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
}
}
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account = completedTask.getResult(ApiException::class.java)
if (account != null) {
Log.e(TAG, "account2==================================\n\n\n")
Log.e(TAG, "account ${account.account}")
Log.e(TAG, "displayName ${account.displayName}")
Log.e(TAG, "email ${account.email}")
Log.e(TAG, "familyName ${account.familyName}")
Log.e(TAG, "givenName ${account.givenName}")
Log.e(TAG, "grantedScopes ${account.grantedScopes}")
Log.e(TAG, "id ${account.id}")
Log.e(TAG, "idToken ${account.idToken}")
Log.e(TAG, "isExpired ${account.isExpired}")
Log.e(TAG, "photoUrl ${account.photoUrl}")
Log.e(TAG, "requestedScopes ${account.requestedScopes}")
Log.e(TAG, "serverAuthCode ${account.serverAuthCode}")
Log.e(TAG, "account2==================================\n\n\n")
} else {
mLoginCallBack?.onLoginError(-2)
}
// Signed in successfully, show authenticated UI.
//updateUI(account)
} catch (e: ApiException) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.e(TAG, "signInResult:failed code=" + e.statusCode + " msg " + e.message)
//updateUI(null)
mLoginCallBack?.onLoginError(e.statusCode)
}
}
/**
* 退出登录
*/
fun signOut() {
mGoogleSignInClient?.signOut()
?.addOnCompleteListener(mActivity) {
// ...
//it.result
}
}
/**
* 断开链接
*/
fun revokeAccess() {
mGoogleSignInClient?.revokeAccess()
?.addOnCompleteListener(mActivity) {
// ...
}
}
}
这篇博客介绍了如何在Android应用中集成Google登录功能,包括导入Google服务依赖,并提供了具体的步骤指南,参考了官方文档,同时提供了一个用于处理Google登录的工具类。

471

被折叠的 条评论
为什么被折叠?



