?接著上一篇封裝的Google荣茫、FaceBook登錄工具類的封裝后,我們這篇來說下實(shí)際項(xiàng)目中的具體使用:
1.當(dāng)然本篇也使用了懶加載场靴,最近剛學(xué)這個啡莉,覺得非常好用,舉個栗子:
private val mCallbackManager by lazy { CallbackManager.Factory.create() }
private val TAG by lazy { "MainActivity" }
private val mGoogleLoginUtils by lazy { GoogleLoginUtils(this, this) }
2.初始化view:
private fun initView() {
? ? tv_google.setOnClickListener {
? ? ? ? checkGoogleService()
? ? }
? ? tv_facebook.setOnClickListener {
? ? ? ? if (!CheckApkExistUtils.checkFacebookExist(this)) {
? ? ? ? ? ? Toast.makeText(this, "請先安裝FaceBook客戶端", Toast.LENGTH_LONG).show()
? ? ? ? } else {
? ? ? ? ? ? mFaceBookLoginUtils.login()
? ? ? ? }
? ? }
? ? tv_twitter.setOnClickListener {
? ? }
}
3.初始化Google和FaceBook:
private fun initGoogle() {
? ? mGoogleLoginUtils.setGoogleSignListener(this)
}
private fun initFaceBook() {
? ? mFaceBookLoginUtils.setFaceBookLoginListener(this)
}
4.添加事件監(jiān)聽:
GoogleLoginUtils.GoogleLoginListener,
? ? FaceBookLoginUtils.FaceBookLoginListener {
? ? private val mFaceBookLoginUtils by lazy {
? ? ? ? FaceBookLoginUtils(
? ? ? ? ? ? this,
? ? ? ? ? ? callbackManager = mCallbackManager
? ? ? ? )
? ? }
? ? private val mCallbackManager by lazy { CallbackManager.Factory.create() }
? ? private val TAG by lazy { "MainActivity" }
? ? private val mGoogleLoginUtils by lazy { GoogleLoginUtils(this, this) }
5.Google登錄的時候需要先檢查是否安裝Googleplay服務(wù):
/**
* 先檢查用戶是否安裝Google服務(wù)
*/
private fun checkGoogleService() {
? ? val code = CheckGoogleServiceUtils.checkGooglePlayServiceExist(this)
? ? if (code == ConnectionResult.SUCCESS) {
? ? ? ? mGoogleLoginUtils.signIn()
? ? } else {
? ? ? ? code.let { CheckGoogleServiceUtils.onCheckGooglePlayServices(this, it) }
? ? }
}
6.FaceBook登錄的時候需要先檢查是否安裝Facebook客戶端,當(dāng)然瀏覽器也可以登錄旨剥,但是沒有App方便咧欣,經(jīng)過和產(chǎn)品商量后直接使用App登錄,方便快捷.
tv_facebook.setOnClickListener {
? ? if (!CheckApkExistUtils.checkFacebookExist(this)) {
? ? ? ? Toast.makeText(this, "請先安裝FaceBook客戶端", Toast.LENGTH_LONG).show()
? ? } else {
? ? ? ? mFaceBookLoginUtils.login()
? ? }
}
7.登錄回調(diào):
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
? ? super.onActivityResult(requestCode, resultCode, data)
? ? Log.d(TAG, resultCode.toString())
? ? if (mCallbackManager != null) {
? ? ? ? mCallbackManager.onActivityResult(requestCode, resultCode, data)
? ? }
? ? if (requestCode == mGoogleLoginUtils.requestCode) {
? ? ? ? val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
? ? ? ? mGoogleLoginUtils.handleSignInResult(task)
? ? }
}
8.處理你自己的登錄請求:
9.完整的Activity代碼:?
package com.powervision.thirdlogindemo
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.facebook.CallbackManager
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.tasks.Task
import com.powervision.thirdlogindemo.bean.FaceBookLoginInfoBean
import com.powervision.thirdlogindemo.utils.CheckApkExistUtils
import com.powervision.thirdlogindemo.utils.CheckGoogleServiceUtils
import com.powervision.thirdlogindemo.utils.FaceBookLoginUtils
import com.powervision.thirdlogindemo.utils.GoogleLoginUtils
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), GoogleLoginUtils.GoogleLoginListener,
? ? FaceBookLoginUtils.FaceBookLoginListener {
? ? private val mFaceBookLoginUtils by lazy {
? ? ? ? FaceBookLoginUtils(
? ? ? ? ? ? this,
? ? ? ? ? ? callbackManager = mCallbackManager
? ? ? ? )
? ? }
? ? private val mCallbackManager by lazy { CallbackManager.Factory.create() }
? ? private val TAG by lazy { "MainActivity" }
? ? private val mGoogleLoginUtils by lazy { GoogleLoginUtils(this, this) }
? ? override fun onCreate(savedInstanceState: Bundle?) {
? ? ? ? super.onCreate(savedInstanceState)
? ? ? ? setContentView(R.layout.activity_main)
? ? ? ? initView()
? ? ? ? initFaceBook()
? ? ? ? initGoogle()
? ? }
? ? private fun initGoogle() {
? ? ? ? mGoogleLoginUtils.setGoogleSignListener(this)
? ? }
? ? private fun initFaceBook() {
? ? ? ? mFaceBookLoginUtils.setFaceBookLoginListener(this)
? ? }
? ? private fun initView() {
? ? ? ? tv_google.setOnClickListener {
? ? ? ? ? ? checkGoogleService()
? ? ? ? }
? ? ? ? tv_facebook.setOnClickListener {
? ? ? ? ? ? if (!CheckApkExistUtils.checkFacebookExist(this)) {
? ? ? ? ? ? ? ? Toast.makeText(this, "請先安裝FaceBook客戶端", Toast.LENGTH_LONG).show()
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? mFaceBookLoginUtils.login()
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? tv_twitter.setOnClickListener {
? ? ? ? }
? ? }
? ? /**
? ? * 先檢查用戶是否安裝Google服務(wù)
? ? */
? ? private fun checkGoogleService() {
? ? ? ? val code = CheckGoogleServiceUtils.checkGooglePlayServiceExist(this)
? ? ? ? if (code == ConnectionResult.SUCCESS) {
? ? ? ? ? ? mGoogleLoginUtils.signIn()
? ? ? ? } else {
? ? ? ? ? ? code.let { CheckGoogleServiceUtils.onCheckGooglePlayServices(this, it) }
? ? ? ? }
? ? }
? ? override fun onLoginSuccess(infoBean: FaceBookLoginInfoBean) {
? ? ? ? //處理Facebook登錄成功后的請求
? ? }
? ? override fun onLoginFailed(message: String) {
? ? }
? ? override fun onLoginCancel(message: String) {
? ? }
? ? override fun onLogoutSuccess() {
? ? }
? ? override fun onLogoutFailed() {
? ? }
? ? override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data)
? ? ? ? Log.d(TAG, resultCode.toString())
? ? ? ? if (mCallbackManager != null) {
? ? ? ? ? ? mCallbackManager.onActivityResult(requestCode, resultCode, data)
? ? ? ? }
? ? ? ? if (requestCode == mGoogleLoginUtils.requestCode) {
? ? ? ? ? ? val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
? ? ? ? ? ? mGoogleLoginUtils.handleSignInResult(task)
? ? ? ? }
? ? }
? ? override fun googleLoginSuccess(account: GoogleSignInAccount) {
? ? ? ? //處理Google登錄成功后的請求
? ? }
? ? override fun googleLoginFail(message: String?) {
? ? }
? ? override fun googleLogoutSuccess() {
? ? }
? ? override fun googleLogoutFail() {
? ? }
}
10.布局文件代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity">
? ? <TextView
? ? ? ? android:id="@+id/tv_google"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="Google登錄"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:background="@color/colorPrimary"
? ? ? ? android:gravity="center"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toLeftOf="parent"
? ? ? ? app:layout_constraintRight_toLeftOf="@id/tv_facebook"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? <TextView
? ? ? ? android:id="@+id/tv_facebook"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="FaceBook登錄"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:background="@color/colorPrimary"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_marginStart="20dp"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_google"
? ? ? ? app:layout_constraintRight_toLeftOf="@id/tv_twitter"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? <TextView
? ? ? ? android:id="@+id/tv_twitter"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="Twitter登錄"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:background="@color/colorPrimary"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_marginStart="20dp"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_facebook"
? ? ? ? app:layout_constraintRight_toRightOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
11.以上就是Google和Facebook登錄的完整流程和代碼轨帜,如有合適的你可以放到自己項(xiàng)目中魄咕,當(dāng)然集成ShareSdk或者友盟可以很好的實(shí)現(xiàn)國外第三方登錄,一鍵集成即可蚌父,不用專門去下載集成每一家的sdk,這里只是說下我做這個的封裝和使用.如有問題哮兰,及時溝通烟具,我們努力改正,謝謝~~
最后放出項(xiàng)目源碼地址: