Google+登錄集成 點擊按鈕標(biāo)準(zhǔn)式配置
1.請科學(xué)打開官網(wǎng)地址
- 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 - 在 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() 方法中檢查 是否已經(jīng)登錄
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() 方法中添加回調(diào)
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)
//添加回調(diào)事件 復(fù)寫成功請求方法
googleLogin?.setGoogleListener(this)
// 登錄事件方法中調(diào)用
googleLogin?.signIn()
//onActivityResult 方法中添加
googleLogin?.onActivityResult(requestCode, resultCode, data)
參考DEMO