Android Google Helper类登录
示例
下面添加到您的build.gradle出android标签:
//将插件应用到应用。 apply plugin: 'com.google.gms.google-services'
将以下帮助器类添加到您的util包中:
/** * Created by Andy */ public class GoogleSignInHelper implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks{ private static final String TAG = GoogleSignInHelper.class.getSimpleName(); private static GoogleSignInHelper googleSignInHelper; private AppCompatActivity mActivity; private GoogleApiClient mGoogleApiClient; public static final int RC_SIGN_IN = 9001; private boolean isLoggingOut = false; public static GoogleSignInHelper newInstance(AppCompatActivity mActivity) { if (googleSignInHelper == null) { googleSignInHelper = new GoogleSignInHelper(mActivity, fireBaseAuthHelper); } return googleSignInHelper; } public GoogleSignInHelper(AppCompatActivity mActivity) { this.mActivity= mActivity; initGoogleSignIn(); } private void initGoogleSignIn() { //[STARTconfig_sign_in] //配置Google登录 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(mActivity.getString(R.string.default_web_client_id)) .requestEmail() .build(); //[ENDconfig_sign_in] mGoogleApiClient = new GoogleApiClient.Builder(mActivity) .enableAutoManage(mActivity /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .addConnectionCallbacks(this) .build(); } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { //发生无法解决的错误,GoogleAPI(包括登录)将无法 //能得到的。 Log.d(TAG, "onConnectionFailed:" + connectionResult); Toast.makeText(mActivity, "GooglePlay服务错误。", Toast.LENGTH_SHORT).show(); } public void getGoogleAccountDetails(GoogleSignInResult result) { //Google登录成功,已通过FireBase进行身份验证 GoogleSignInAccount account = result.getSignInAccount(); //您现在已登录Google } public void signOut() { if (mGoogleApiClient.isConnected()) { //Google登出 Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { isLoggingOut = false; } }); } else { isLoggingOut = true; } } public GoogleApiClient getGoogleClient() { return mGoogleApiClient; } @Override public void onConnected(@Nullable Bundle bundle) { Log.w(TAG, "onConnected"); if (isLoggingOut) { signOut(); } } @Override public void onConnectionSuspended(int i) { Log.w(TAG, "onConnectionSuspended"); } }
将以下代码添加到您OnActivityResult的活动文件中:
//[STARTonactivityresult] @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //从GoogleSignInApi.getSignInIntent(...)启动Intent返回的结果; if (requestCode == GoogleSignInHelper.RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { googleSignInHelper.getGoogleAccountDetails(result); } else { //Google登录失败,请适当更新用户界面 //[START_EXCLUDE] Log.d(TAG, "signInWith Google failed"); //[END_EXCLUDE] } } } //[ENDonactivityresult] //[开始登录] public void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleSignInHelper.getGoogleClient()); startActivityForResult(signInIntent, GoogleSignInHelper.RC_SIGN_IN); } //[END登录]