Android中Google、FaceBook登錄工具類的實際使用

?接著上一篇封裝的Google、FaceBook登錄工具類的封裝后,我們這篇來說下實際項目中的具體使用:

1.當然本篇也使用了懶加載,最近剛學(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客戶端,當然瀏覽器也可以登錄,但是沒有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登錄的完整流程和代碼,如有合適的你可以放到自己項目中,當然集成ShareSdk或者友盟可以很好的實現(xiàn)國外第三方登錄,一鍵集成即可,不用專門去下載集成每一家的sdk,這里只是說下我做這個的封裝和使用.如有問題,及時溝通,我們努力改正,謝謝~~

最后放出項目源碼地址:

ThirdLoginDemo: 使用Kotin封裝的Google、FaceBook、Twitter登錄工具類

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容