1.build.gradle文件中添加
dependencies {
//GooglePlay登陆
implementation ‘com.google.android.gms:play-services-auth:16.0.1’
}
2.初始化onCreate中调用
FtGooglePlayLogin.initLogin(activity);
3.点击的地方调用登录
public static String loginGooglePlay() {//GooglePlay登录
if(((FtPay)Manager.getInstance().ftPay).activity!=null) {
FtGooglePlayLogin.doLoginGooglePlay(((FtPay)Manager.getInstance().ftPay).activity);
}
return null;
}
4.登录回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
FtGooglePlayLogin.onActivityResult(requestCode, resultCode, data);//谷歌 登录回调
}
5.新建
FtGooglePlayLogin.java类
代码如下:
package com.ftaro.adapter;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
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.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
public class FtGooglePlayLogin {
private static GoogleSignInClient mGoogleSignInClient;
public static final int GoogleResoultCode = 10000;//谷歌
private static String GoogleAppID=“s29302”;
public static void initLogin( final Activity activity){
//TODO Google登录
//配置登录以请求用户的ID,电子邮件地址和基本配置文件.ID和基本配置文件包含在DEFAULT_SIGN_IN中.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(GoogleAppID)
.build();
//使用gso指定的选项构建一个GoogleSignInClient.
mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
}
public static void doLoginGooglePlay( final Activity activity){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
activity.startActivityForResult(signInIntent, GoogleResoultCode);
}
public static void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==GoogleResoultCode) {
//方式一
// GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
// handleSignInResult1(result);
//方式二
Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult2(task);
}
}
private static void handleSignInResult1(GoogleSignInResult result){
Log.d(“google_login”,“handleSignInResult方法调用----” + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
//account里可以直接获取到ID,name,和头像
//Log.d( “google_login”,“id-” + account.getId() + “name-” + account.getDisplayName() + “photo-” + account.getPhotoUrl());
String idToken = account.getIdToken();
Log.d(“google_login”,“Token-----”+idToken+"-----");
Manager.thirdName2=account.getId();
}
else {
Manager.thirdName2=“1”;
}
}
private static void handleSignInResult2(Task completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
//account里可以直接获取到ID,name,和头像
Log.d(“google_login”,“id--------” + account.getId() + “----name----” + account.getDisplayName() + “—photo–” + account.getPhotoUrl());
String idToken = account.getIdToken();
Log.d(“google_login”,“Token-----” + idToken + “-----”);
Manager.thirdName2=account.getId();
} catch (ApiException e) {
Log.d(“google_login”, “signInResult:failed code=” + e.getStatusCode());
Log.d(“google_login”,“错误信息为” + e.toString());
Manager.thirdName2=“1”;
}
}
}