Google+登录集成 点击按钮标准式配置
1.请科学打开官网地址
WechatIMG16.jpeg- Add Google Play services
In your project's top-level build.gradle file, ensure that Google's Maven repository is included:
allprojects {
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url 'https://maven.google.com'
// }
}
}
Then, in your app-level build.gradle file, declare Google Play services as a dependency:
implementation 'com.google.android.gms:play-services-auth:16.0.1'
检查AS是否下载Google Play services
WechatIMG17.jpeg
- 在 Activity 类 onCreate()方法 中添加如下配置
In your sign-in activity'sonCreate
method, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users' ID and basic profile information, create aGoogleSignInOptions
object with theDEFAULT_SIGN_IN
parameter. To request users' email addresses as well, create theGoogleSignInOptions
object with therequestEmail
option.
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
If you need to request additional scopes to access Google APIs, specify them with requestScopes
. For the best user experience, on sign-in, only request the scopes that are required for your app to minimally function. Request any additional scopes only when you need them, so that your users see the consent screen in the context of an action they performed. See Requesting Additional Scopes.
4.onStart() 方法中检查 是否已经登录
In your activity's onStart method, check if a user has already signed in to your app with Google.
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
// 已登录 account 不为空
- layout 文件中添加googleButton 使用自己写的也是可以的。
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
6.点击事件
In the activity's onClick
method, handle sign-in button taps by creating a sign-in intent with the getSignInIntent
method, and starting the intent with startActivityForResult
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
- onActivityResult() 方法中添加回调
After the user signs in, you can get aGoogleSignInAccount
object for the user in the activity'sonActivityResult
method.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 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.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
到此Google 第三方登录已集成完毕!
附:完整代码
/**
* @author BuMingYang
* @des 谷歌登录
*/
class GoogleLogin(private var context: Activity) {
private var mGoogleSignInClient: GoogleSignInClient? = null
private var googleListener: GoogleListener? = null
private val RC_SIGN_IN = 9001
init {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(context, gso)
}
//google
fun signIn() {
val signInIntent = mGoogleSignInClient?.signInIntent
context.startActivityForResult(signInIntent, RC_SIGN_IN)
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode === RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
}
}
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
var account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
// Signed in successfully, show authenticated UI.
googleListener?.onGoogleSuccess("${account?.id}")
} catch (e: ApiException) {
Log.e("Login", "signInResult:failed code=" + e.statusCode)
context.showSnackMsg("signInResult:failed code=${e.statusCode}")
}
}
fun setGoogleListener(googleListener: GoogleListener) {
this.googleListener = googleListener
}
interface GoogleListener {
/**
* 登录成功
* @param id
*/
fun onGoogleSuccess(id: String)
}
Activity 中使用 (项目代码 避免嫌疑 提供主要使用地方)
//初始化
googleLogin = GoogleLogin(this)
//添加回调事件 复写成功请求方法
googleLogin?.setGoogleListener(this)
// 登录事件方法中调用
googleLogin?.signIn()
//onActivityResult 方法中添加
googleLogin?.onActivityResult(requestCode, resultCode, data)
参考DEMO
网友评论