Conditions for testing Google login:
1. The mobile phone has the ability to bypass the wall.
2. The mobile phone is a Google mobile phone or has Google services installed .
/sign-in/android/sign-in integration steps: Step 1: Register and log in to the google mobile platform Step 2: Add your awesome project and set the region Step 3 : After creating the project, select the integration platform Step 4 : Select Android, set the package name, sha1 and other information Step 5 : Register the application, then remember to download the json file in the jumped page , and put the json file in the main module root directory Step 6 : Project-level build.gradle (/build.gradle): buildscript { dependencies { // Add this line classpath 'com.google.gms:google-services:3.2.0' } }
App-level build.gradle:
dependencies {
// Add this line
compile 'com.google.firebase:firebase-core:12.0.1'
}
…
// Add to the bottom of the file
apply plugin: 'com.google.gms. google-services'
Step 7: The environment configuration is completed, Sync Now...
Step 8: The sdk needs to be downloaded google_play_services
Step 9: Define the initialization callback interface, and define the login callback interface:
public interface GoogleInitListener {
void initFinish(GoogleSignInAccount account,GoogleSignInClient client );
}
public interface GoogleLoginListener {
void loginSuccess(GoogleSignInAccount account);
void LoginFailed(String msg);
}
Step 10: Click the google login button to start initialization:
String google_app_id = "your app id";
public void gooleAuthInit( Activity activity,GoogleInitListener googleInitListener){
GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(googleId)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
//如果用戶已经登录返回account对象,没有登录 返回null
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(activity);
if(account==null){
//初始化完成,检查到google账户没有登录
googleInitListener.initFinish(null,mGoogleSignInClient);
}else{
//初始化完成,检查到google账户已经登录,返回账号信息
googleInitListener.initFinish(account,mGoogleSignInClient);
}
}
Step 11: When receiving the initialization callback object, call login:
private GoogleLoginListener mGoogleAuthListener;
//打开google登录页
public void gooleAuthLogin(Activity activity, GoogleSignInClient mGoogleSignInClient,GoogleLoginListener googleAuthListener){
mGoogleAuthListener = googleAuthListener;
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
activity.startActivityForResult(signInIntent,RC_SIGN_IN);
}
//登录结果返回
public void onActivityResult(int requestCode, int resultCode, Intent 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
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.
mGoogleAuthListener.loginSuccess(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());
Log.d(TAG, e.getMessage());
e.printStackTrace();
mGoogleAuthListener.LoginFailed(e.getStatusCode()+","+e.getMessage());
}
}
This is the end of the login process. You are welcome to leave comments, criticisms and corrections, thank you!