android Studio接入googleplay 登录

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”;
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值