Android使用Kotlin封装Google等海外登录工具类

Android使用Kotlin封装Google等海外登录工具类

前言:

之前由于项目做国际化,所以需要接入Google、FaceBook、Twitter等各种第三方登录的Api,于是查找官网资料,申请各种key和密钥,最后成功完成了这三个第三方登录Api的功能,但是由于很多地方需要调用,前期做得很粗糙,发现调用很麻烦,于是做了一次封装,总结一下,关于资料的申请这里就不说了.如果是海外或者有vpn的小伙伴可以集成各种key后直接测试,国内你懂的. 本文采用的是Kotlin语法,直接上封装的代码:

1.配置google依赖:

arduino 复制代码
// google
"play-services-auth"           : 'com.google.android.gms:play-services-auth:19.0.0',
"play-services-wallet"         : 'com.google.android.gms:play-services-wallet:18.1.2',
"play-services-base"           : 'com.google.android.gms:play-services-base:17.6.0',
"play-services-analytics"      : 'com.google.android.gms:play-services-analytics:17.0.0',
"google-pay"                   : 'com.android.billingclient:billing:4.0.0',
"google-ar"                    : 'com.google.ar:core:1.11.0',

2.配置FaceBook:

arduino 复制代码
// Facebook Core only (Analytics)
"facebook-core"                : 'com.facebook.android:facebook-core:8.1.0',
// Facebook Login only
"facebook-login"               : 'com.facebook.android:facebook-login:8.1.0',
// Facebook Share only
"facebook-share"               : 'com.facebook.android:facebook-share:8.1.0',
// Facebook Messenger only
"facebook-messenger"           : 'com.facebook.android:facebook-messenger:8.1.0',
// Facebook App Links only
"facebook-applinks"            : 'com.facebook.android:facebook-applinks:8.1.0',
// Facebook Android SDK (everything)
"facebook-android-sdk"         : 'com.facebook.android:facebook-android-sdk:8.1.0',

3.配置Twitter:

arduino 复制代码
//twitter
"twitter"                      : 'com.twitter.sdk.android:twitter:3.3.0',
"tweet-composer"               : 'com.twitter.sdk.android:tweet-composer:3.3.0',
"colorfulText"                 : 'org.zhx.common:colorfulText:1.0.0'

4.在build.gradle导入依赖:

php 复制代码
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    api rootProject.ext.dependencies["facebook-login"]
    api rootProject.ext.dependencies["facebook-core"]
    api rootProject.ext.dependencies["play-services-auth"]
    api rootProject.ext.dependencies["play-services-wallet"]
    api rootProject.ext.dependencies["twitter"]
    api rootProject.ext.dependencies["tweet-composer"]
}

5.接入Google、Facebook、Twitter:

Google、Facebook、Twitter之前需要安装Google服务,所以首先需要判断用户是否安装Google服务插件,这里说特别说明一下,一定要安装官网的正版Google服务插件,要不然会出现各种问题,这里就不展开了.检测用户是否安装Google服务的方法如下:

kotlin 复制代码
/**
 * 检查用户是否安装Google Play 服务
 */
open fun onCheckGooglePlayService(activity:Activity,code:Int){
// 验证是否已在此设备上安装并启用Google Play服务,以及此设备上安装的旧版本是否为此客户端所需的版本
GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(activity)
/**
 * 通过isUserResolvableError来确定是否可以通过用户操作解决错误
 */
if (GoogleApiAvailability.getInstance().isUserResolvableError(code)) {
    GoogleApiAvailability.getInstance().getErrorDialog(activity, code, 200).show()
}
​
fun checkGooglePlayServiceExist(activity: Activity): Int {
    return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
}

6.Google登录工具类的封装:

kotlin 复制代码
/**
 * @author: njb
 * @date: 2021/5/8 14:37
 * @desc: Google登录工具类
 */
open class GoogleLoginUtils(private val activity: Activity, var listener: GoogleSignListener) {
    var requestCode = 10
    private var mGoogleSignInClient: GoogleSignInClient? = null
    private var googleSignListener: GoogleSignListener? = null

    init {
        //初始化谷歌登录服务
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build()
        mGoogleSignInClient = GoogleSignIn.getClient(activity, gso)
    }

    /**
     * 登录
     */
    open fun signIn() {
        val signInIntent: Intent = mGoogleSignInClient!!.signInIntent
        activity.startActivityForResult(signInIntent, requestCode)
    }

    /**
     * 退出登录
     */
    open fun signOut() {
    }


    fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
        try {
            val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
            val status: Task<GoogleSignInAccount> = completedTask
            if (status.isSuccessful) {
                if (account != null) {
                    //ToastUtils.showShort(activity.getString(R.string.login_success))
                    googleSignListener?.googleLoginSuccess(account)
                    LogUtils.d("account" + account?.account)
                } else {
                    googleSignListener?.googleLoginFail(activity.getString(R.string.login_failed))
                }
            }
        } catch (e: ApiException) {
            LogUtils.d("signInResult:failed code=" + e.statusCode)
        }
    }


    open fun setGoogleSignListener(googleSignListener: GoogleSignListener?) {
        this.googleSignListener = googleSignListener
    }

    interface GoogleSignListener {
        fun googleLoginSuccess(account: GoogleSignInAccount)
        fun googleLoginFail(message: String?)
        fun googleLogoutSuccess()
        fun googleLogoutFail()
    }

    /**
     * 检查 Google Play 服务
     */
    open fun onCheckGooglePlayServices(activity: Activity, code: Int) {
        // 验证是否已在此设备上安装并启用Google Play服务,以及此设备上安装的旧版本是否为此客户端所需的版本
        GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(activity)
        /**
         * 通过isUserResolvableError来确定是否可以通过用户操作解决错误
         */
        if (GoogleApiAvailability.getInstance().isUserResolvableError(code)) {
            GoogleApiAvailability.getInstance().getErrorDialog(activity, code, 200).show()
        }
    }

    fun checkGooglePlayServiceExist(activity: Activity): Int {
        return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
    }
}

7.FaceBook登录工具类的封装:

kotlin 复制代码
/**
 *@author: njb
 *@date:   2021/5/8 14:26
 *@desc:   FaceBook登录工具类
 */
open class FacebookLoginUtils(private val activity: Activity) {
    private var loginManager: LoginManager? = null
    private val permissions: List<String>
    private var callbackManager: CallbackManager? = null
    private var requestSuccessCode = 2896
    private var requestCancelCode = 2897
    private var requestFailedCode = 2898
    private var mFaceBookLoginListener: FaceBookLoginListener? = null
    var infoBean: FaceLoginInfoBean? = null

    init {
        //初始化facebook登录服务
        callbackManager = CallbackManager.Factory.create()
        getLoginManager().registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
            override fun onSuccess(result: LoginResult?) {
                activity.setResult(requestSuccessCode)
                getLoginInfo(result?.accessToken)
            }

            override fun onCancel() {
                activity.setResult(requestCancelCode)
                mFaceBookLoginListener?.onLoginCancel(activity.getString(R.string.login_cancel))
            }

            override fun onError(error: FacebookException?) {
                activity.setResult(requestFailedCode)
                mFaceBookLoginListener?.onLoginFail(error?.message)
                LogUtils.d(error?.message)
            }
        })
        permissions = listOf("email", "user_likes", "user_status", "user_photos", "user_birthday", "public_profile", "user_friends");
    }

    /**
     * 登陆
     */
    fun login() {
        getLoginManager().logIn(activity, permissions)
    }

    /**
     * 退出登陆
     */
    fun loginOut() {
        var logout = activity.resources.getString(R.string.com_facebook_loginview_log_out_action)
        var cancel = activity.resources.getString(R.string.com_facebook_loginview_cancel_action)
        val message: String
        val profile: Profile = Profile.getCurrentProfile()
        message = if (profile?.name != null) {
            java.lang.String.format(
                    activity.resources.getString(
                            R.string.com_facebook_loginview_logged_in_as),
                    profile.name)
        } else {
            activity.resources.getString(
                    R.string.com_facebook_loginview_logged_in_using_facebook)
        }
        val builder: AlertDialog.Builder = AlertDialog.Builder(activity)
        builder.setMessage(message)
                .setCancelable(true)
                .setPositiveButton(logout) { _, _ ->
                    getLoginManager().logOut()
                }
                .setNegativeButton(cancel, null)
        builder.create().show()
    }

    /**
     * 获取登录信息
     * @param accessToken
     */
    private fun getLoginInfo(accessToken: AccessToken?) {
        var request = GraphRequest.newMeRequest(accessToken, object : GraphRequest.GraphJSONObjectCallback {
            override fun onCompleted(`object`: JSONObject?, response: GraphResponse?) {

                `object`?.run {
                    val id = optString("id") //比如:13813813888
                    val name = optString("name") //比如:Zhang San
                    val gender = optString("gender") //性别:比如 male (男)  female (女)
                    val email = optString("email") //邮箱:比如:11111@qq.com
                    //获取用户头像
                    val object_pic = optJSONObject("picture")
                    val object_data = object_pic.optJSONObject("data")
                    val photo = object_data.optString("url")
                    //获取地域信息
                    val locale = optString("locale") //zh_CN 代表中文简体
                    infoBean = FaceLoginInfoBean()
                    infoBean?.id = id
                    infoBean?.name = name
                    infoBean?.email = email
                    infoBean?.gender = gender
                    infoBean?.picture = object_pic.toString()
                    infoBean?.data = object_data.toString()
                    infoBean?.photo = photo
                    infoBean?.locale = locale
                    infoBean?.let { mFaceBookLoginListener?.onLoginSuccess(it) }
                }
            }
        })
        val parameters = Bundle()
        parameters.putString("fields", "id,name,link,gender,birthday,email,picture,locale,updated_time,timezone,age_range,first_name,last_name")
        request.parameters = parameters
        request.executeAsync()
    }

    fun getCallbackManager(): CallbackManager? {
        return callbackManager
    }

    /**
     * 获取loginMananger
     * @return
     */
    private fun getLoginManager(): LoginManager {
        if (loginManager == null) {
            loginManager = LoginManager.getInstance();
        }
        return loginManager!!
    }

    companion object {
        /**
         * 是否处在登陆状态
         */
        fun isLogin(): Boolean {
            val accessToken = AccessToken.getCurrentAccessToken()
            val isLoggedIn = accessToken != null && !accessToken.isExpired
            return isLogin()
        }
    }
​
    open fun setFaceBookLoginListener(faceBookLoginListener: FaceBookLoginListener?) {
        this.mFaceBookLoginListener = faceBookLoginListener
    }

    interface FaceBookLoginListener {
        fun onLoginSuccess(infoBean: FaceLoginInfoBean)
        fun onLoginCancel(message: String?)
        fun onLoginFail(message: String?)
        fun onLogoutSuccess()
        fun onLogoutFail()
    }
}

8.Twitter登录工具类的封装:

kotlin 复制代码
**
 * @author: njb
 * @date: 2021/5/14 10:08
 * @desc: Twitter登录工具类
 */
class TwitterLoginUtils(activity: Activity) {
        private var activityRef: WeakReference<Activity>? = null
        var callback: Callback<TwitterSession>? = null

    @Volatile
    var authClient: TwitterAuthClient? = null

    companion object {
        const val ERROR_MSG_NO_ACTIVITY = ("TwitterLoginButton requires an activity."
                + " Override getActivity to provide the activity for this button.")
    }

    init {
        activityRef = WeakReference(activity)
        TwitterCore.getInstance()
    }

    fun setOnLoginByTwitterClick(callback: Callback<TwitterSession>?) {
        this.callback = callback
        checkCallback(callback)
        checkActivity(activityRef?.get())
        twitterAuthClient!!.authorize(activityRef?.get(), callback)
    }

    private val twitterAuthClient: TwitterAuthClient?
        get() {
            if (authClient == null) {
                synchronized(TwitterLoginButton::class.java) {
                    if (authClient == null) {
                        authClient = TwitterAuthClient()
                    }
                }
            }
            return authClient
        }

    private fun checkCallback(callback: Callback<*>?) {
        if (callback == null) {
            CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG,
                    "Callback must not be null, did you call setCallback?")
        }
    }

    private fun checkActivity(activity: Activity?) {
        if (activity == null || activity.isFinishing) {
            CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG, ERROR_MSG_NO_ACTIVITY)
        }
    }

    /**
     * Call this method when [Activity]
     * is called to complete the authorization flow.
     *
     * @param requestCode the request code used for SSO
     * @param resultCode  the result code returned by the SSO activity
     * @param data        the result data returned by the SSO activity
     */
    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == twitterAuthClient!!.requestCode) {
            twitterAuthClient!!.onActivityResult(requestCode, resultCode, data)
        }
    }
}

9.简单使用代码如下:

使用具体步骤如下:

9.1先判断用户是否安装Google服务

9.2先声明工具类

csharp 复制代码
private var mGoogleLogin: GoogleLoginUtils? = null
private var mFacebookLoginUtils: FacebookLoginUtils? = null

9.3Google和FaceBook工具类的初始化

kotlin 复制代码
private fun initGoogle() {
    mGoogleLogin = GoogleLoginUtils(this, this)
    mGoogleLogin?.setGoogleSignListener(this)
}
​
private fun initFaceBook() {
    mFacebookLoginUtils = FacebookLoginUtils(this)
    mFacebookLoginUtils?.setFaceBookLoginListener(this)
}

9.4 Google、FaceBook调用不同的方法:

scss 复制代码
R.id.iv_google -> {//先检查是否安装Google服务
    checkGoogleService()
}
R.id.iv_face_book -> {//检查是否安装FaceBook客户端
    if (!CheckApkExist.checkFacebookExist(this)) {
        ToastUtils.showShort(getString(com.jiaoday.base.R.string.please_install_face_book_first))
    } else {
        mFacebookLoginUtils?.login()
    }
}
kotlin 复制代码
private fun checkGoogleService() {
    val code = mGoogleLogin?.checkGooglePlayServiceExist(this)
    if (code == ConnectionResult.SUCCESS) {
        mGoogleLogin?.signIn()
    } else {
        code?.let { mGoogleLogin?.onCheckGooglePlayServices(this, it) }
    }
}

9.5Google和FaceBook登录回调:

在最开始继承一下登录回调监听方法:

GoogleLoginUtils.GoogleSignListener, FacebookLoginUtils.FaceBookLoginListener

9.6登录成功、失败、取消登录后的回调

kotlin 复制代码
override fun googleLoginSuccess(account: GoogleSignInAccount) {
    //google登录成功后返回的信息
    if (account.email != null) {
        thirdAccountLogin(account.email.toString(), account.email.toString(), "4")
    }
}

override fun googleLoginFail(message: String?) {
    ToastUtils.showShort(message)
}

override fun googleLogoutSuccess() {
}

override fun googleLogoutFail() {
}

override fun onLoginSuccess(infoBean: FaceLoginInfoBean) {

//faceBook登录成功后返回的信息

if (infoBean.email.isNotEmpty()) {

thirdAccountLogin(infoBean.email, infoBean.email, "1") }

}

override fun onLoginCancel(message: String?) { ToastUtils.showShort(message) }

override fun onLoginFail(message: String?) { ToastUtils.showShort(message) }

override fun onLogoutSuccess() { }

override fun onLogoutFail() { }

9.7 onActivityResult回调结果:

kotlin 复制代码
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == mGoogleLogin?.requestCode) {
        val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
        mGoogleLogin?.handleSignInResult(task)
    }
    mFacebookLoginUtils?.getCallbackManager()?.onActivityResult(requestCode, resultCode, data)
}

10. 懒加载:

kotlin中的懒加载很安逸,使用起来非常简单方便,举个栗子:

kotlin 复制代码
private val mCallbackManager by lazy { CallbackManager.Factory.create() }
private val TAG by lazy { "MainActivity" }
private val mGoogleLoginUtils by lazy { GoogleLoginUtils(this, this) }

11.完整的使用流程如下:

  • 初始化view.
  • 初始化Google和FaceBook.
  • 添加事件监听.
  • 检查googlePlay服务.
  • FaceBook登录时检查是否安装FaceBook客户端.
  • 添加登录回调(登录成功、失败、取消登录等).

12.初始化view:

kotlin 复制代码
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 {

    }
}

13.初始化Google和FaceBook:

kotlin 复制代码
private fun initGoogle() {
    mGoogleLoginUtils.setGoogleSignListener(this)
}

private fun initFaceBook() {
    mFaceBookLoginUtils.setFaceBookLoginListener(this)
}

14.添加事件监听:

kotlin 复制代码
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) }

15.先检查是否安装Googleplay服务:

kotlin 复制代码
/**
 * 先检查用户是否安装Google服务
 */
private fun checkGoogleService() {
    val code = CheckGoogleServiceUtils.checkGooglePlayServiceExist(this)
    if (code == ConnectionResult.SUCCESS) {
        mGoogleLoginUtils.signIn()
    } else {
        code.let { CheckGoogleServiceUtils.onCheckGooglePlayServices(this, it) }
    }
}

16.FaceBook登录时先检查是否安装客户端

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

17.登录回调:

kotlin 复制代码
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)
    }
}

18.完整Activity测试代码:

kotlin 复制代码
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() {
    }
}

19.布局代码如下:

ini 复制代码
<?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>

20.总结:

以上就是Google、Facebook、Twitter登录工具类的完整封装和使用流程,如有合适的你可以放到自己项目中,当然集成ShareSdk或者友盟可以很好的实现国外第三方登录,一键集成即可,不用专门去下载集成每一家的sdk,这里我做这个的封装是为了能够更好地使用,方便其他同事和项目,后面还可以做统一封装,只需要传递相应类型,登录成功和失败的方法回调出来就可以了.如有问题,及时沟通,我会努力改正,一起学习,共同进步,谢谢大家~~

21.项目源码地址如下:

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

相关推荐
萌面小侠Plus37 分钟前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农37 分钟前
Android Profiler 内存分析
android
大风起兮云飞扬丶38 分钟前
Android——多线程、线程通信、handler机制
android
L725644 分钟前
Android的Handler
android
清风徐来辽1 小时前
Android HandlerThread 基础
android
HerayChen2 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
顾北川_野2 小时前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
hairenjing11232 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小黄人软件2 小时前
android浏览器源码 可输入地址或关键词搜索 android studio 2024 可开发可改地址
android·ide·android studio
dj15402252033 小时前
group_concat配置影响程序出bug
android·bug