• Android中Google、FaceBook登录工具类的实际使用


     接着上一篇封装的Google、FaceBook登录工具类的封装后,我们这篇来说下实际项目中的具体使用:

    1.当然本篇也使用了懒加载,最近刚学这个,觉得非常好用,举个栗子:

    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.添加事件监听:

    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服务:

    /**
     * 先检查用户是否安装Google服务
     */
    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方便,经过和产品商量后直接使用App登录,方便快捷.

    tv_facebook.setOnClickListener {
        if (!CheckApkExistUtils.checkFacebookExist(this)) {
            Toast.makeText(this, "请先安装FaceBook客户端", Toast.LENGTH_LONG).show()
        } else {
            mFaceBookLoginUtils.login()
        }
    }

    7.登录回调:

    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服务
         */
        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或者友盟可以很好的实现国外第三方登录,一键集成即可,不用专门去下载集成每一家的sdk,这里只是说下我做这个的封装和使用.如有问题,及时沟通,我们努力改正,谢谢~~

    最后放出项目源码地址:

    ThirdLoginDemo: 使用Kotin封装的Google、FaceBook、Twitter登录工具类


  • 相关阅读:
    64位ubuntu中chrome浏览器安装Adobe Flashplayer插件
    DirectX SDK 重大版本变化记录[转]
    DirectX SDK版本与Visual Studio版本 [转]
    C#正则表达式整理备忘[转]
    NPOI 设置单元格边框
    C# Winform DirectX视频播放器
    C#压缩指定的文件并生成zip文件
    maple 15 数学图形绘制软件[VeryCD]
    VWG部署到64位win7系统的关键
    VWG网页下载时中文乱码的解决方法
  • 原文地址:https://www.cnblogs.com/ning137/p/15498043.html
Copyright © 2020-2023  润新知